go Flashcards

(44 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 start a goroutine that calls function f?

A

go f()

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

Code:

```go
____ f() // launch concurrently
~~~

A

go

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

What Go type is used to synchronize concurrent goroutines waiting for completion?

A

sync.WaitGroup

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

Fill the blank to send value v on channel ch: \_\_\_\_

A

ch <- v

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

Fill the blank to receive from channel ch into x: x := \_\_\_\_

A

<-ch

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

What happens when you close a channel?

A

Receivers drain remaining buffered values then receive zero values; sends panic.

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

Idiomatic way to avoid blocking send when channel is full?

A

Use select { case ch <- v: default: }

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

How do you create a buffered channel of int with capacity 4?

A

ch := make(chan int, 4)

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

Which type provides an efficient mutable bytes buffer?

A

bytes.Buffer

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

Code completion: write “hello” to buffer b

```go
var b bytes.Buffer
b.____(“hello”)
~~~

A

WriteString

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

Copy all data from r to w: ____

A

io.Copy(w, r)

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

Quickly build strings without reallocations:

A

strings.Builder

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

Start a basic HTTP server on :8080 serving mux:

A

log.Fatal(http.ListenAndServe(“:8080”, mux))

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

Create GET request with context ctx to url u:

A

req, err := http.NewRequestWithContext(ctx, http.MethodGet, u, nil)

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

Defer closing response body resp:

A

defer resp.Body.Close()

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

Marshal struct s to JSON bytes:

A

b, err := json.Marshal(s)

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

Unmarshal JSON bytes b into variable v:

A

err := json.Unmarshal(b, &v)

19
Q

Add json tag to field Name in struct:

A

Name string json:”name”``

20
Q

Make []int of len 0 cap 10:

A

s := make([]int, 0, 10)

21
Q

Append value x to slice s:

A

s = append(s, x)

22
Q

Map lookup idiom for key k in m, getting ok flag:

A

v, ok := m[k]

23
Q

Declare method String() string on pointer *T:

A

func (t *T) String() string { … }

24
Q

Blank identifier purpose _:

A

Ignore value or import side‑effect

25
Interface satisfaction rule:
Implicit: type implements interface by having required methods
26
Idiomatic error check after call returning error err:
if err != nil { return err }
27
Wrap an error with context:
fmt.Errorf("context: %w", err)
28
Check if err is io.EOF:
errors.Is(err, io.EOF)
29
Create cancellable context child of ctx:
ctx, cancel := context.WithCancel(ctx)
30
Timeout a goroutine after 5s using select and time.After:
select { case <-done: case <-time.After(5*time.Second): }
31
Add 1 to WaitGroup wg before launching goroutine:
wg.Add(1)
32
Mark WaitGroup done in goroutine:
defer wg.Done()
33
Ensure init() runs exactly once:
var once sync.Once; once.Do(init)
34
Set max OS threads to n:
runtime.GOMAXPROCS(n)
35
Run only TestFoo in package tests:
go test -run TestFoo
36
Read entire file my.txt to bytes:
b, err := os.ReadFile("my.txt")
37
Use bufio.Scanner to read file line by line: fill blank ```go scanner := bufio.NewScanner(____) ```
file
38
Add build tag 'netgo' at top of file:
//go:build netgo
39
Purpose of default case in select:
Non‑blocking select path if no other cases ready.
40
What is a goroutine leak?
Goroutines blocked forever on channel/IO so never exit.
41
Allow multiple readers but single writer lock type:
`sync.RWMutex`
42
Embed type Logger in struct S:
type S struct { *log.Logger }
43
Signature of io.Reader's Read:
Read(p []byte) (n int, err error)
44
Combine outputs from multiple channels into one: pattern name?
Fan‑in