Go Collection Flashcards

1
Q

What is an Array ?

A

A collection with fixed length and similar datatype.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the long syntax of declaring array in GO ?

A

var <arrayName> [<arrayLength>]<dataType>
e.g. var myArray [3]int</dataType></arrayLength></arrayName>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How to initialise a declared array ?

A

Using the index.
e.g. myArray[1] = 12

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the short syntax of creating and initialisation of array in a single line ?

A

myArray := [4]int{1,2,3,4}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What’s a slice ?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How to initialise an empty array ?

A

It can be initialised with empty Curly braces.
e.g. myArray := [3]int{}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Is it mandatory to initialise variable in shorthand operator ?>

A

Yes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How to create an empty slice in Go ?

A

Same syntax of Array without the length.
e.g. mySlice := []int{}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How to append a new element in existing slice ?

A

Built-in “append” function:
e.g. mySlice = append(mySlice, 23, 445)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How to get a “slice” of an array ?

A

By providing the upper index and lower index:
e.g. mySlice := myArray[ 3:8]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How to create slice of an array for all elements ?

A

DO NOT provide the lower and upper index
e.g. mySlice := myArray[:]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How to create a Map in Go ?

A

Using “map” keyword and providing datatype of both “key” and “value”:
e.g. myMap := map[string]int{}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How to initialise a Map during declaration ?

A

By Passing JSON like structure in “key” value format:
e.g. myMap := map[string]{“key”:12}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How to get a value from Map, after providing the Key ?

A

myMap[“key”]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How to re-assign a new Value to existing key in a map ?

A

myMap[“key”] = value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How to delete any key-value from a Map ?

A

Using inbuilt “delete” function.
e.g. delete(myMap, “key”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What do you mean by “Homogeneity” of “Arrays, Slices and Maps”

A

Arrays, Slices and Maps are homogeneous, which means they can only hold similar data types.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Which collection in Go is “heterogeneous” ?

A

Struct (just like classes, it can have any type of fields and functions)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What is Struct ?

A

A Struct is a heterogeneous collection, which can hold data of different types.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

How to declare a “Struct” ?

A

By using “Type” and “Struct” keyword:
e.g.
type user struct {
id int,
firstName string,
lastName string
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

How to initialise a struct ?

A

It can be initialised just like a map:
e.g. u := user{ id: 1, firstName: “Nandan”, lastName: “singh”}

22
Q

What’s a package in a module ?

A

A package is a collection of source files which shares unique responsibility within a module.

23
Q

How to import a package with module ?

A

Using “Fully qualified Name”:
e.g. import “github.com/myCompany/myRepo/myPackage”

24
Q

How to declare a function ?

A

Using “Func” keyword:
func MyFunction(){
fmt.Println(“using function”)
}

25
How to invoke a function ?
By using "Parenthesis" e.g. myFunction()
26
How to pass arguments to any function ?
func myFunction( param1 int, param2 string){ fmt.Println("Using params", param1, param2) }
27
Do we need to provide "return Type" in Go functions ?
Yes
28
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") }
29
What is the data type required for return the exceptions in GO?
"error"
30
How to create a new Error value ?
By calling the New function of "errors" package >> err := errors.New("something bad happend")
31
How to throw an error from a function?
func myFunction() error { return errors.New("something bad happend") }
32
Can we provide multiple return types in Go function ?
yes, a single function can return the multiple values.
33
How to return multiple values from a function ?
By providing multiple "return types" separated by comma: func myFunc() (int, error) { return 23, nil }
34
How to consume multiple values returned by a function. ?
By declaring variables equals to the return values >>> myValue, err := myFunc()
35
How to consume only selected return values, if a function returns multiple values ?
Just declare the variable as "underscore". >>> _ , _ := myFunction()
36
Can we use "short assignment" operation outside the function ?
No
37
How to create variable outside the function ?
Use Long assignment and declaration >>> var myVar int = 1
38
What is a "variable" block ?
Multiple variable declaration collected inside the parenthesis without repeating the word "var". >>> var ( myVar1 int myVar2 string )
39
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++
40
Does golang support both "pre-fix" and "post-fix" increment and decrement operator ?
No, it only supports Post Fix, "increment" and "decrement" is supported.
41
Can we add a "behaviour" to any custom data type in GO ?
Yes, by using Methods.
42
Does Go supports class ?
No, Go-lang doesn't supports classes, it only supports Types.
43
What are "methods" in GO ?
Methods are the functions with special "receiver argument". This argument "binds" the function with the Type.
44
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 }
45
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.
46
Does Go supports constructor ?
No
47
What's a constructor ?
A constructor is a special kind of function, whose sole responsibility is to "initialise the object" when it first formed.
48
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
49
Declare a constructor function for type "user" ?
type User struct { firstName, lastName string } func newUser(){ return user{ "Nandan", "Singh"} }
50
How to start a server in Go?
By using the Method "ListenAndServer" method of "http" pacakge
51