Home Interview Questions and AnswersTechnical Interview Questions and AnswersGo Go Interview Questions and Answers For Freshers Part-5

Go41.How to define a slice in Go?
To define a slice, you can declare it as an array without specifying size or use make function to create the one.

var numbers []int /* a slice of unspecified size */
/* numbers == []int{0,0,0,0,0}*/
numbers = make([]int,5,5) /* a slice of length 5 and capacity 5*/

42.How to get the count of elements present in a slice?
len() function returns the elements presents in the slice.

43.What is the difference between len() and cap() functions of slice in Go?
len() function returns the elements presents in the slice where cap() function returns the capacity of slice as how many elements it can be accomodate.

44.How to get a sub-slice of a slice?
Slice allows lower-bound and upper bound to be specified to get the subslice of it using[lower-bound:upper-bound].

45.What is range in Go?
The range keyword is used in for loop to iterate over items of an array, slice, channel or map. With array and slices, it returns the index of the item as integer. With maps, it returns the key of the next key-value pair.

46.What are maps in Go?
Go provides another important data type map which maps unique keys to values. A key is an object that you use to retrieve a value at a later date. Given a key and a value, you can strore the value in a Map object. After value is stored, you can retrieve it by using its key.

47.How to create a map in Go?
You must use make function to create a map.

/* declare a variable, by default map will be nil*/
var map_variable map[key_data_type]value_data_type
/* define the map as nil map can not be assigned any value*/
map_variable = make(map[key_data_type]value_data_type)

48.How to delete an entry from a map in Go?
delete() function is used to delete an entry from the map. It requires map and corresponding key which is to be deleted.

49.What is type casting in Go?
Type casting is a way to convert a variable from one data type to another data type. For example, if you want to store a long value into a simple integer then you can type cast long to int. You can convert values from one type to another using the cast operator as following:

type_name(expression)

50.What are interfaces in Go?
Go programming provides another data type called interfaces which represents a set of method signatures. struct data type implements these interfaces to have method definitions for the method signature of the interfaces.

You may also like

Leave a Comment