A Tour of Go - Basics: More types: structs, slices, and maps. Flashcards

1
Q

Does Go have pointers?

A

Go has pointers. A pointer holds the memory address of a value.

The type *T is a pointer to a T value. Its zero value is nil.

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

What is the address operator?

A

The & generates a pointer to it’s operand

i := 42
p := &i
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the derefence operator?

A

The * operator denotes the pointer’s underlying value.

fmt.Println(*p)
*p = 21
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Does Go support pointer arithmetic like ‘C’?

A

No.

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

How is a struct declared in Go?

A

A struct is a collection of fields

type Vertex struct {
	X int
	Y int
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How are struct fields accessed?

A

Using a dot

var v Vertex
v.X = 1
x.Y = 5
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How are fields accessed with a struct pointer?

A

To access the field X of a struct when we have the struct pointer p we could write (*p).X. However, that notation is cumbersome, so the language permits us instead to write just p.X, without the explicit dereference.

v := &Vertex{}
(*v).X = 1
v.Y = 5
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are struct literals?

A

A struct literal denotes a newly allocated struct value by listing the values of its fields.

You can list just a subset of fields by using the Name: syntax. (And the order of named fields is irrelevant.)

The special prefix & returns a pointer to the struct value.

v1 = Vertex{1, 2}  // has type Vertex
v2 = Vertex{X: 1}  // Y:0 is implicit
v3 = Vertex{}      // X:0 and Y:0
p  = &Vertex{1, 2} // has type *Vertex
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How are arrays declared?

A

The type [n]T is an array of n values of type T.

var a [10]int
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

An array’s length is ….

A

An array’s length is part of its type, so arrays cannot be resized.

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

What is a slice?

A

An array has a fixed size. A slice, on the other hand, is a dynamically-sized, flexible view into the elements of an array.

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

How is a slice declared?

A

The type []T is a slice with elements of type T.

primes := [6]int{2, 3, 5, 7, 11, 13}

var s []int = primes[1:4]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is a slice literal?

A

A slice literal is like an array literal without the length.

r := []bool{true, false, true}

This creates an array then builds a slice that references it.

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

What are the defaults for a slice lower bound?

A

zero

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

What is the default for a slice upper bound?

A

The length of the array - len(s)

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

What is the length of a slice?

A

The length of a slice is the number of elements it contains.

len(s)
17
Q

What is the capacity of the slice?

A

The capacity of a slice is the number of elements in the underlying array, counting from the first element in the slice.

cap(s)
18
Q

Whats the zero value of a slice?

A

The zero value of a slice is nil.

A nil slice has a length and capacity of 0 and has no underlying array.

19
Q

In Go, how can you create a dynamically-sized array?

A

Use the built-in make function. The make function allocates a zeroed array and returns a slice that refers to that array:

a := make([]int, 5) // len(a)=5
20
Q

What is the 3rd argument to the built-in `make function?

A

To specify a capacity, pass a third argument to make:

b := make([]int, 0, 5) // len(b)=0, cap(b)=5
21
Q

What types can a slice contain?

A

Any, including other slices.

// Create a tic-tac-toe board.
board := [][]string{
    []string{"_", "_", "_"},
    []string{"_", "_", "_"},
    []string{"_", "_", "_"},
}
22
Q

How can you append elements to a slice?

A

Go provides a built-in function append. The first parameter s of append is a type T, and the rest are T values to append to the slice.

If the backing array of s is too small to fit all the given values a bigger array will be allocated. The returned slice will point to the newly allocated array.

23
Q

How do you iterate over a slice?

A

The range form of the for loop iterates over a slice.

When ranging over a slice, two values are returned for each iteration. The first is the index, and the second is a copy of the element at that index.

for i, v := range s {
    fmt.Printf("s[%d] = %d\n", i, v)
}
24
Q

With a range for loop can you skip the index or value?

A

You can skip the index or value by assigning to _.

for i, _ := range pow
for _, value := range pow

If you only want the index, you can omit the second variable.

for i := range pow
25
Q

In Go, what is a map?

A

A map maps keys to values.

The zero value of a map is nil. A nil map has no keys, nor can keys be added.

The make function returns a map of a given type initialised and ready for use.

var m map[string]string
m = make(map[string]string)
26
Q

What is a map literal?

A

A map literals are like struct literals, but the keys are required.

var m = map[string]Vertex{
	"Bell Labs": Vertex{
		40.68433, -74.39967,
	},
	"Google": Vertex{
		37.42202, -122.08408,
	},
}
27
Q

What can you omit from a map literal?

A

If the top-level type is just a type name, you can omit it from the elements of the literal.

var m = map[string]Vertex{
	"Bell Labs": {40.68433, -74.39967},
	"Google":    { 37.42202, -122.08408},
}
28
Q

How do you insert or update a map element?

A
m[key] = elem
29
Q

How do you retrieve a map element?

A
elem = m[key]
30
Q

How do you delete a map element

A

delete(m, key)

31
Q

How do you test a map key is present?

A

A two-value assignment can be used:

elem, ok = m[key]

If key is in m, ok is true. if not ok is `false.

if key is not in the map, then elem is the zero value for the map’s element type.

32
Q

In Go, are functions values?

A

Yes. They can be passed around just like other values.

Function values may be used as function arguments and return values.

func compute(fn func(float64, float64) float64) float64 {
	return fn(3, 4)
}

func main() {
	hypot := func(x, y float64) float64 {
		return math.Sqrt(x*x + y*y)
	}
	fmt.Println(hypot(5, 12))

	fmt.Println(compute(hypot))
	fmt.Println(compute(math.Pow))
}
33
Q

What is a function closure?

A

A closure is a function value that references variables from outside it’s body. The function may access and assign to the referenced variables; in this sense the function is “bound” to the variables.

34
Q

Give an example of a closure?

A
func adder() func(int) int {
	sum := 0
	return func(x int) int {
		sum += x
		return sum
	}
}

func main() {
	pos, neg := adder(), adder()
	for i := 0; i < 10; i++ {
		fmt.Println(
			pos(i),
			neg(-2*i),
		)
	}
}
35
Q

Complete the fibonacci closure:

func fibonacci() func() int {
}

func main() {
	f := fibonacci()
	for i := 0; i < 10; i++ {
		fmt.Println(f())
	}
}
A
func fibonacci() func() int {
	a, b := 0, 1
	return func() int {
		f := a
		a, b = a+b, f
		return f
	}
}