Go Collection Flashcards
What is an Array ?
A collection with fixed length and similar datatype.
What is the long syntax of declaring array in GO ?
var <arrayName> [<arrayLength>]<dataType>
e.g. var myArray [3]int</dataType></arrayLength></arrayName>
How to initialise a declared array ?
Using the index.
e.g. myArray[1] = 12
What is the short syntax of creating and initialisation of array in a single line ?
myArray := [4]int{1,2,3,4}
What’s a slice ?
Slice is a collection of similar datatypes, whose length is not fixed.
It gets expand as new elements gets added and shrink as older elements gets removed.
How to initialise an empty array ?
It can be initialised with empty Curly braces.
e.g. myArray := [3]int{}
Is it mandatory to initialise variable in shorthand operator ?>
Yes.
How to create an empty slice in Go ?
Same syntax of Array without the length.
e.g. mySlice := []int{}
How to append a new element in existing slice ?
Built-in “append” function:
e.g. mySlice = append(mySlice, 23, 445)
How to get a “slice” of an array ?
By providing the upper index and lower index:
e.g. mySlice := myArray[ 3:8]
How to create slice of an array for all elements ?
DO NOT provide the lower and upper index
e.g. mySlice := myArray[:]
How to create a Map in Go ?
Using “map” keyword and providing datatype of both “key” and “value”:
e.g. myMap := map[string]int{}
How to initialise a Map during declaration ?
By Passing JSON like structure in “key” value format:
e.g. myMap := map[string]{“key”:12}
How to get a value from Map, after providing the Key ?
myMap[“key”]
How to re-assign a new Value to existing key in a map ?
myMap[“key”] = value
How to delete any key-value from a Map ?
Using inbuilt “delete” function.
e.g. delete(myMap, “key”)
What do you mean by “Homogeneity” of “Arrays, Slices and Maps”
Arrays, Slices and Maps are homogeneous, which means they can only hold similar data types.
Which collection in Go is “heterogeneous” ?
Struct (just like classes, it can have any type of fields and functions)
What is Struct ?
A Struct is a heterogeneous collection, which can hold data of different types.
How to declare a “Struct” ?
By using “Type” and “Struct” keyword:
e.g.
type user struct {
id int,
firstName string,
lastName string
}
How to initialise a struct ?
It can be initialised just like a map:
e.g. u := user{ id: 1, firstName: “Nandan”, lastName: “singh”}
What’s a package in a module ?
A package is a collection of source files which shares unique responsibility within a module.
How to import a package with module ?
Using “Fully qualified Name”:
e.g. import “github.com/myCompany/myRepo/myPackage”
How to declare a function ?
Using “Func” keyword:
func MyFunction(){
fmt.Println(“using function”)
}
How to invoke a function ?
By using “Parenthesis”
e.g. myFunction()
How to pass arguments to any function ?
func myFunction( param1 int, param2 string){
fmt.Println(“Using params”, param1, param2)
}
Do we need to provide “return Type” in Go functions ?
Yes
How to provide return “data Type” in Function ?
After “parenthesis” and before curly braces in Function declaration:
func myFunc( param1, param2 int) int {
fmt.Println(“hey”)
}
What is the data type required for return the exceptions in GO?
“error”
How to create a new Error value ?
By calling the New function of “errors” package
» err := errors.New(“something bad happend”)
How to throw an error from a function?
func myFunction() error {
return errors.New(“something bad happend”)
}
Can we provide multiple return types in Go function ?
yes, a single function can return the multiple values.
How to return multiple values from a function ?
By providing multiple “return types” separated by comma:
func myFunc() (int, error) {
return 23, nil
}
How to consume multiple values returned by a function. ?
By declaring variables equals to the return values
»>
myValue, err := myFunc()
How to consume only selected return values, if a function returns multiple values ?
Just declare the variable as “underscore”.
»> _ , _ := myFunction()
Can we use “short assignment” operation outside the function ?
No
How to create variable outside the function ?
Use Long assignment and declaration
»> var myVar int = 1
What is a “variable” block ?
Multiple variable declaration collected inside the parenthesis without repeating the word “var”.
»> var (
myVar1 int
myVar2 string
)
Can we use “increment/decrement operator” inside a statement ?
No, increment and decrement operators are statement in itself. Hence, it should be used in a it’s own line.
»> myNum := 1
myNum++
Does golang support both “pre-fix” and “post-fix” increment and decrement operator ?
No, it only supports Post Fix, “increment” and “decrement” is supported.
Can we add a “behaviour” to any custom data type in GO ?
Yes, by using Methods.
Does Go supports class ?
No, Go-lang doesn’t supports classes, it only supports Types.
What are “methods” in GO ?
Methods are the functions with special “receiver argument”.
This argument “binds” the function with the Type.
How to declare a method in Go?
// create custom Type
type User struct { firstName, lastName string}
// create method (or behaviour)
func (user User) getFullName string {
return user.firstName + “ “ + user.lastName
}
How to convert any function into a Method ?
By “Binding” it with a function.
The binding is done by “receiver argument”. It simply means, the object itself is passed to the function during runtime.
Does Go supports constructor ?
No
What’s a constructor ?
A constructor is a special kind of function, whose sole responsibility is to “initialise the object” when it first formed.
What are the convention/practice needs to be followed to create a constructor function ?
Practice/Properties:
1. It starts with “new” word followed by the Type which it needs to initialise
2. It is always ZERO argument
3. It always returns the “type” which it initialises
Declare a constructor function for type “user” ?
type User struct { firstName, lastName string }
func newUser(){
return user{ “Nandan”, “Singh”}
}
How to start a server in Go?
By using the Method “ListenAndServer” method of “http” pacakge