More go Flashcards

(24 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 define an anonymous function that returns 42?

A

f := func() int { return 42 }

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

What’s the Go idiom to defer a function that recovers from panic?

A

defer func() { if r := recover(); r != nil { … } }()

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

What happens when you loop with range over a slice and capture the loop variable by reference?

A

All closures share the same variable, leading to unexpected results.

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

Pass a context with timeout of 1s:

A

ctx, cancel := context.WithTimeout(context.Background(), time.Second)

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

What should you do after creating a cancellable context?

A

Defer the cancel function to avoid leaks.

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

Omit empty field Name from JSON if it’s empty string:

A

Name string json:”name,omitempty”``

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

Difference between Marshal and Encoder.Encode?

A

Marshal returns []byte; Encode writes to an io.Writer.

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

What happens when you read a nonexistent key from a map?

A

You get the zero value of the value type.

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

Can you take the address of a map element directly?

A

No, because map elements are not addressable.

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

Command to tidy your go.mod file:

A

go mod tidy

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

Command to upgrade all direct dependencies:

A

go get -u ./...

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

Write a test function for Sum() in package main:

A

func TestSum(t *testing.T) { ... }

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

How to run tests with race detector?

A

go test -race

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

What does interface{} represent in Go?

A

The empty interface: can hold values of any type.

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

How do you assert the type of value v held in interface{}?

A

x, ok := v.(TargetType)

17
Q

What happens if you call Lock() twice without Unlock()?

18
Q

Difference between Mutex and RWMutex?

A

RWMutex allows multiple readers or one writer; Mutex allows only one locker.

19
Q

What does io.EOF signify in Go?

A

That no more input is available (not necessarily an error).

20
Q

Read only the first 100 bytes from file f:

A

io.LimitReader(f, 100)

21
Q

How to leak-proof a goroutine that listens on a channel?

A

Close the channel or use context cancellation.

22
Q

When is an init() function run in Go?

A

Before the main() function, once per package.

23
Q

Write a compile-time interface satisfaction check for T:

A

var _ MyInterface = (*T)(nil)

24
Q

Why might you use sync.Pool?

A

To reuse temporary objects and reduce GC pressure.