Go Cheat Sheet Page 2023-07-08 https://www.andy-pearce.com ...
Go Cheat Sheet
Page 1 of 6
Last modi ed: 2023-09-30
? Copyright 2023 Andy Peace --
Distributed under MIT license --
Tooling
go env go help environment
Show all Go settings Show purpose of all Go settings
Development
go run go get @ go list -m all go mod why -m go clean -modcache gofmt -d -w -r `abc -> xyz' go doc go doc -all go doc -src
Runs executable in directory Fetch dependency optionally at version Show all dependencies Why does this module depend on ? Clean the module cache Replace abc with xyz Display summary documentation for Display full documentation for Display source code for
Testing
go test go test /... go test -race ... go test -count= ... go clean -testcache go test -v -run= ... go test -short ... go test -failfast ... go test -fuzz ... go test -fuzz -fuzztime= ... go test -cover ... go test -coverprofile= go test -covermode=count ... go tool cover -html= go tool cover -func=
Runs all tests in directory Runs all tests in and subdirectories Run tests with data race detector Bypass test cache and run tests times Clean up all cached test results Run all tests matching regular expression Skips tests lagged as long-running Stop running tests on rst failure Run any de ned fuzz tests until a failure is found or aborted Run any de ned fuzz tests until a failure is found or expires Show code coverage summary Generate coverage metrics in output le Additionally capture the number of times each line is hit Show report from recorded coverage metrics in browser Show report from recorded coverage metrics in terminal
Latest version: Go logo copyright 2018 The Go Authors, licensed under Creative Commons Attribution 4.0.
if
i
f
i if f f
if
Go Cheat Sheet
Page 2 of 6
Last modi ed: 2023-07-08
? Copyright 2023 Andy Peace --
Distributed under MIT license --
Tidying Up
go fmt ./... go vet go mod tidy go mod verify
Reformat this and all subdirectories Run static code analysis on le or directory Remove unused dependencies Verify that dependencies are still clean
Building
go build -o=
Build package in optionally placing output in
go build -a ...
Bypass cache and rebuild all packages
go clean -cache
Clean entire build cache
GOOS=linux GOARCH=amd64 go build ... Specify operating system and architecture for cross-compilation
go tool dist list
List all valid OS/arch targets for this compiler
go build -gcflags="" ...
Pass to the compiler for packages listed on command-line
... -gcflags="=" ...
Pass to the compiler for all packages matching
go build -ldflags="" ...
Pass to the linker
Ongoing Maintenance
go test -bench= ... -benchmem ... -benchtime= ... -count= ... -cpu= go test -cpuprofile= go test -memprofile= go test -blockprofile= go test -mutexprofile= go tool pprof ... -top -functions ... ... -top -lines ... ... -http localhost: ... go list -u -m all go get @ go mod tidy && go test all
Runs tests and benchmarks from Shows memory allocations for each iteration as well as execution time Sets the threshold time (e.g. 5s) or number of iterations (e.g. 500x) Repeats each benchmark times in sequence Runs each benchmark with GOMAXPROCS at each value in list Run tests outputting CPU time pro ling data to Run tests outputting memory allocation pro ling data to Run tests outputting blocking call pro ling data to Run tests outputting mutex contention pro ling data to Run interactive CLI on executable with pro ling data Show top users at granularity of functions Show top users at granularity of source code lines Start interactive web interface on and open a browser on it For each dependency, show current version and upstream latest Also used to update existing dependency version Good idea to do this a ter updating dependencies
Latest version: Go logo copyright 2018 The Go Authors, licensed under Creative Commons Attribution 4.0.
if
i
f if
i
f
if
if
f
if
Go Cheat Sheet
Page 3 of 6
Last modi ed: 2023-07-08
? Copyright 2023 Andy Peace --
Distributed under MIT license --
Scalar Types
bool (true / false)
uint uint8 uint16 uint32 uint64
string
int int8 int16 int32 int64
float32 complex64
float64 complex128
uintptr rune (int32) byte (uint8)
Pointer & Channel Types
var ptr *int
var value int = 99
ptr = &value
value = *ptr
unbuffered := make(chan int)
buffered := make(chan int, 100)
chan 0 { ... i -= 1
}
for { ... break
}
Functions & Closures
for i := 0; i < 10; i++ { ...
}
for i, val := range items { ...
}
func name(arg1 int, arg2 string) int { ... return 123
func name(arg string) (string, error) { ... return "", errors.New("Foo")
func name(arg int, more ...string) { for i, value := range more { ... } anotherFunc(arg, more...)
}
Panic, Defer & Recover
func name() (someVar int) { someVar = 123 return
}
func ticketMachine(start int) func() int { return func() int { start++ return start }
}
panic(value)
func someFunc(arg int) { ... } Func otherFunc() {
defer someFunc(123) ... }
Predeclared Identi ers
func name() {
defer func() { if r := recover(); r != nil { ... }
}() ...
}
any bool byte comparable error complex64 complex128 float32 float64 int int8 int16 int32 int64 rune string uint uint8 uint16 uint32 uint64 uintptr
true false iota nil
append cap close complex copy delete imag len make new panic print println real recover
Latest version: Go logo copyright 2018 The Go Authors, licensed under Creative Commons Attribution 4.0.
i
f
if
Go Cheat Sheet
Page 5 of 6
Last modi ed: 2023-07-08
? Copyright 2023 Andy Peace --
Distributed under MIT license --
Methods
type Point struct { x, y float64
} func (p *Point) move(dx, dy float64) {
p.x += dx p.y += dy }
Value type T T *T *T
Receiver type T *T T *T
Callable?
Interfaces
type Shape interface { area() float64 translate(x, y float64)
}
func f(s Shape) float64 { s.translate(1.0, 5.0) return s.area()
}
type Cornered interface { Shape numCorners() int
}
All types implement the empty interface interface{} type SignedInt interface { ~int | ~int8 | ~int16 | ~int32 | ~int64
The ~ means "same underlying type", without it's an exact match }
Generics
type List[T any] struct { ...
}
func (node *List[T]) insertAfter(item T) *List[T] { ...
}
func find[T comparable](n T, h []T) int { ...
}
func newList[T any](item T) *List[T] { return &List[T]{item, nil, nil}
}
Go Routines & Channels
func consumer(c ................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related searches
- https www municipalonlinepayments
- cheat sheet for word brain game
- macro cheat sheet pdf
- logarithm cheat sheet pdf
- excel formula cheat sheet pdf
- excel formulas cheat sheet pdf
- excel cheat sheet 2016 pdf
- vba programming cheat sheet pdf
- macro cheat sheet food
- free excel cheat sheet download
- onenote cheat sheet pdf
- punctuation rules cheat sheet pdf