basics Flashcards

(32 cards)

1
Q

Front

A

Back

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

How do you declare a variable in Go?

A

var x int = 42

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

What’s the short variable declaration syntax in Go?

A

x := 42

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

How do you define a function in Go?

A

func add(a int, b int) int { return a + b }

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

How do you define a struct in Go?

A

type Person struct { Name string; Age int }

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

How do you write a for loop in Go?

A

for i := 0; i < 10; i++ { fmt.Println(i) }

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

How do you create a slice from an array in Go?

A

slice := arr[1:4]

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

How do you return multiple values from a function in Go?

A

a, b := myFunc()

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

How do you define an interface in Go?

A

type Reader interface { Read(p []byte) (n int, err error) }

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

How do you write a switch statement in Go?

A

switch x := time.Now().Weekday(); x { case time.Saturday: … }

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

How do you defer a function call in Go?

A

defer fmt.Println(“done”)

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

Front

A

Back

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

How do you declare a variable in Go?

A

var x int = 42

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

What’s the short variable declaration syntax in Go?

A

x := 42

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

How do you define a function in Go?

A

func add(a int, b int) int { return a + b }

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

How do you define a struct in Go?

A

type Person struct { Name string; Age int }

17
Q

How do you write a for loop in Go?

A

for i := 0; i < 10; i++ { fmt.Println(i) }

18
Q

How do you create a slice from an array in Go?

A

slice := arr[1:4]

19
Q

How do you return multiple values from a function in Go?

A

a, b := myFunc()

20
Q

How do you define an interface in Go?

A

type Reader interface { Read(p []byte) (n int, err error) }

21
Q

How do you write a switch statement in Go?

A

switch x := time.Now().Weekday(); x { case time.Saturday: … }

22
Q

How do you defer a function call in Go?

A

defer fmt.Println(“done”)

23
Q

How do you declare a constant in Go?

A

const Pi = 3.14

24
Q

How do you create a map in Go?

A

m := map[string]int{“foo”: 1, “bar”: 2}

25
How do you check if a key exists in a map?
val, ok := m["key"]
26
How do you define an anonymous function?
f := func(x int) int { return x * x }
27
What does the blank identifier `_` do in Go?
It ignores a value, often used to discard return values.
28
How do you create a goroutine?
go someFunction()
29
How do you create a channel?
ch := make(chan int)
30
How do you send and receive values on a channel?
ch <- 1 (send), val := <-ch (receive)
31
What is the purpose of `select` in Go?
It waits on multiple channel operations.
32
How do you recover from a panic?
Use `defer` and `recover()` inside a function.