go Flashcards
(44 cards)
Front
Back
How do you start a goroutine that calls function f?
go f()
Code:
```go
____ f() // launch concurrently
~~~
go
What Go type is used to synchronize concurrent goroutines waiting for completion?
sync.WaitGroup
Fill the blank to send value v on channel ch: \_\_\_\_
ch <- v
Fill the blank to receive from channel ch into x: x := \_\_\_\_
<-ch
What happens when you close a channel?
Receivers drain remaining buffered values then receive zero values; sends panic.
Idiomatic way to avoid blocking send when channel is full?
Use select { case ch <- v: default: }
How do you create a buffered channel of int with capacity 4?
ch := make(chan int, 4)
Which type provides an efficient mutable bytes buffer?
bytes.Buffer
Code completion: write “hello” to buffer b
```go
var b bytes.Buffer
b.____(“hello”)
~~~
WriteString
Copy all data from r to w: ____
io.Copy(w, r)
Quickly build strings without reallocations:
strings.Builder
Start a basic HTTP server on :8080 serving mux:
log.Fatal(http.ListenAndServe(“:8080”, mux))
Create GET request with context ctx to url u:
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u, nil)
Defer closing response body resp:
defer resp.Body.Close()
Marshal struct s to JSON bytes:
b, err := json.Marshal(s)
Unmarshal JSON bytes b into variable v:
err := json.Unmarshal(b, &v)
Add json tag to field Name in struct:
Name string
json:”name”``
Make []int of len 0 cap 10:
s := make([]int, 0, 10)
Append value x to slice s:
s = append(s, x)
Map lookup idiom for key k in m, getting ok flag:
v, ok := m[k]
Declare method String() string on pointer *T:
func (t *T) String() string { … }
Blank identifier purpose _
:
Ignore value or import side‑effect