Effective Go Flashcards

Based on the book Effective Go (42 cards)

1
Q

How do you verify the expected output from Go Example code?

A

Use an output comment after the print code. e.g.

func ExampleURL() {
    u, err := url.Parse("http://foo.com/go")
    if err != nil {
        log.Fatal(err)
    }
    u.Scheme = "https"
    fmt.Println(u)
    // Output:
    // http://foo.com/go
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How is unordered expected output handled in Example code?

A

Where the output is in a random order you can use the following comment:

func ExamplePerm() {
    r := rand.New(rand.NewSource(time.Now().UnixNano()))
    for _, v := range r.Perm(3) {
        fmt.Println(v)
    }
    // Unordered output:
    // 2
    // 0
    // 1
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Give the commands that generate a coverage profile and then view it in your default browser

A
go test -coverprofile cover.out
go tool cover -html=cover.out
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How can you toggle a view of unit test coverage with the vim-go plugin

A

:GoCoverageToggle
<leader>c

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

Generate a coverage profile and save it to a file named cover.out

A

go test -coverageprofile cover.out

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

View the coverage profile from a the file cover.out in a browser

A

go tool cover -html=cover.out

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

Show the test coverage in percentage without needing a coverage profile file

A

go test -cover

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

Give the command to run benchmarks

A

go test -bench .

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

What method can you call to report allocations in a benchmark

A

b.ReportAllocs()

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

What commands can be used to comparing benchmark results

A
$ go install golang.org/x/perf/cmd/benchstat@latest
go test -bench . -count 10 > old.txt
...optimise code...
go test -bench . -count 10 > new.txt
benchstat old.txt new.txt
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What command runs a benchmark test 10 times

A

go test -bench . -count 10

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

Give an example of an import statement

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

Give and example of a factored import statement

A

import (
“fmt”
“math”
)

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

What are exported names

A

In Go, a name is exported if it begins with a capital letter.

When importing a package, you can refer only to its exported names.

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

Declare a function to add two values x and y

A
func add(x, y int) int {
    return x + y
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is a naked return value

A

A return statement without arguments returns the named return values.

Naked returns should only be used in short functions.

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

What is a var statement

A

The var statement declares a list of variables. The type is last. e.g.

var x, y, z int
18
Q

What is an initialiser

A

When declaring a variable you can also set it’s initial value. e.g.

var i int = 1

If an initialiser is present, the type can be omitted. e.g.

var i = 1
19
Q

What is a short variable declaration

A

Inside a function, the := short assignment statement can be used in place of a var declaration with implicit type. e.g.

i := 1
c := true
c, python, java := true, false, "no!"
20
Q

What are Go’s basic types

A

~~~
bool

string

int int8 int16 int32 int64
uint uint8 uint16 uint32 uint64 uintptr

byte // alias for uint8

rune // alias for int32; represents a Unicode code point

float32 float64

complex64 complex128

21
Q

What does the format verb %q output

A

Prints a double-quoted string safely escaped with Go syntax

22
Q

What does the format verb %v print

A

The value in a default format.

23
Q

What does the format verb %+v print

A

Where the value is a struct, the plus flag adds field names.

24
Q

What does the format verb %#v print

A

a Go-syntax representation of the value

25
What does the format verb %T print
a Go-syntax representation of the type of the value
26
What does the format verb %t print
the work `true` or `false`
27
What third-party package is useful for comparing complex types
`go-cmp`
28
What is a _raw string literal_
This is a string delimited by backticks. The Go compiler does not interpret what is in a raw string.
29
Give the command to print the Go Operating System and Go Architecture
``` go env GOOS GOARCH ```
30
Give the command to print a list of all operating systems that yo can compile
``` go tool dist list -json ```
31
Build for the Linux OS on the AMD64 architecture
``` GOOS=linux GOARCH=amd64 go build ```
32
What is a _closure_
A closure is a function value that references variables from outside its body.
33
What is a variadic func
A variadic func accepts variable number of input values - zero or more. Ellipsis (three-dots) prefix in front of an input type makes a func variadic. ``` func f(names ... string) ```
34
Give an example of a simple variadic func
``` func toFullname(names ...string) string { return strings.Join(names, " ") } ```
35
TIP: A simple API is...
A simple API is better than a complicated one. Hide complexity behind a simple API.
36
TIP: Concurrency is an...
Concurrency is an implementation detail. Design a synchronous API and let the users of your package decide when to use concurrency or not. For example, the Go standard library's API is mostly synchronous.
37
TIP: twins
It's a common practice to create twins. `Do` is like an entry point and handles higher-level stuff, while do handles the specifics.
38
TIP: `time.Since` returns...
`time.Since` returns the time duration between time values. It's a shortcut for `time.Now.Sub(t)`
39
What is a concurrent pipeline?
A pipeline is an extensible and efficient design pattern consisting of concurrent stages. Each stage does something and sends a message to the next via channels.
40
Tip: Using directional channels...
Using directional channels (recieve-only and send-only) prevents you from introducing bugs in the future and show the intentions of what to do (and what you cannot and should not do) with a channel.
41
TIP: Only the channel's owner..
Only the channel's owner should close a channel to ensure all values are sent.
42
The _Orchestrator Function_ pattern
Separating a function that contains the main logic from the goroutine that would run it.