Effective Go Flashcards
Based on the book Effective Go
How do you verify the expected output from Go Example code?
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 is unordered expected output handled in Example code?
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 }
Give the commands that generate a coverage profile and then view it in your default browser
go test -coverprofile cover.out go tool cover -html=cover.out
How can you toggle a view of unit test coverage with the vim-go plugin
:GoCoverageToggle
<leader>c
Generate a coverage profile and save it to a file named cover.out
go test -coverageprofile cover.out
View the coverage profile from a the file cover.out
in a browser
go tool cover -html=cover.out
Show the test coverage in percentage without needing a coverage profile file
go test -cover
Give the command to run benchmarks
go test -bench .
What method can you call to report allocations in a benchmark
b.ReportAllocs()
What commands can be used to comparing benchmark results
$ 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
What command runs a benchmark test 10 times
go test -bench . -count 10
Give an example of an import statement
import "fmt"
Give and example of a factored import statement
import (
“fmt”
“math”
)
What are exported names
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.
Declare a function to add two values x and y
func add(x, y int) int { return x + y }
What is a naked return value
A return statement without arguments returns the named return values.
Naked returns should only be used in short functions.
What is a var
statement
The var
statement declares a list of variables. The type is last. e.g.
var x, y, z int
What is an initialiser
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
What is a short variable declaration
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!"
What are Go’s basic types
~~~
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
What does the format verb %q output
Prints a double-quoted string safely escaped with Go syntax
What does the format verb %v print
The value in a default format.
What does the format verb %+v print
Where the value is a struct, the plus flag adds field names.
What does the format verb %#v print
a Go-syntax representation of the value
What does the format verb %T print
a Go-syntax representation of the type of the value
What does the format verb %t print
the work true
or false
What third-party package is useful for comparing complex types
go-cmp
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.
Give the command to print the Go Operating System and Go Architecture
go env GOOS GOARCH
Give the command to print a list of all operating systems that yo can compile
go tool dist list -json
Build for the Linux OS on the AMD64 architecture
GOOS=linux GOARCH=amd64 go build
What is a closure
A closure is a function value that references variables from outside its body.
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)
Give an example of a simple variadic func
func toFullname(names ...string) string { return strings.Join(names, " ") }
TIP: A simple API is…
A simple API is better than a complicated one. Hide complexity behind a simple API.
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.
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.
TIP: time.Since
returns…
time.Since
returns the time duration between time values. It’s a shortcut for time.Now.Sub(t)
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.
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.
TIP: Only the channel’s owner..
Only the channel’s owner should close a channel to ensure all values are sent.
The Orchestrator Function pattern
Separating a function that contains the main logic from the goroutine that would run it.