rel:: [[Programming Languages]]
# golang
<https://go.dev/doc/install>
## Releases
- 1.20
- [wrapping multiple errors with Join](https://lukas.zapletalovi.com/posts/2022/wrapping-multiple-errors/)
- 1.19
- [A Guide to the Go Garbage Collector](https://readwise.io/reader/shared/01gnfhz8cfdsjtc08r4bc24grj)
- [runtime improvements over the past 4 years](https://readwise.io/reader/shared/01gndyzj64yd23an25g17b7y50)
- [1.18](https://go.dev/doc/go1.18)
- [new things refresher, modules etc](https://itnext.io/version-1-18-refresh-for-go-programmers-f1b0fcfa3b4a) ([devonlink](x-devonthink-item://B9EA0F4D-ADC0-46E8-A3D3-0F6F8116ECC6))
- generics
- [tutorial](https://go.dev/doc/tutorial/generics)
- [Generics can make your go code slower](https://readwise.io/reader/shared/01gj1wkdbgw0h25bypx1p137vk)
- [Crimes with Go Generics](https://readwise.io/reader/shared/01gnd9dfa2svmaaeqrtsdg355v)
- [profiling improvements](https://felixge.de/2022/02/11/profiling-improvements-in-go-1.18/) by [[Felix Geisendörfer]]
- 1.13+
- [error wrapping, Is, As](https://earthly.dev/blog/golang-errors/)
- [Working with Errors in Go 1.13](https://go.dev/blog/go1.13-errors)
## Tools
- [go performance tools cheat sheet](x-devonthink-item://6FAB2B6E-4A08-4AB1-9E02-F4A26ACC93C5)
- [better structured concurrency for go - conc](https://github.com/sourcegraph/conc)
- [fgprof: A Sampling Profiler for Go](https://golangweekly.com/link/90876/rss) — [pprof](https://golangweekly.com/link/90877/rss) is the go-to tool for profiling Go code, but [it only profiles ‘on-CPU’ time](https://golangweekly.com/link/90878/rss). fgprof can work alongside pprof and measure ‘off-CPU’ time too (such as that spent on IO) so you can get the whole picture. [[Performance]]
- [trubka](https://github.com/xitonix/trubka) a [[cli]] tool for [[kafka]] [[Stream Processing]]
- [gopter](https://github.com/leanovate/gopter) [[property-based testing]]
- [[pprof]] [[Profiling]] [[Performance]]
- [Proposal: hardware performance counters for CPU profiling](https://go.googlesource.com/proposal/+/refs/changes/08/219508/2/design/36821-perf-counter-pprof.md)
- related to "[fgprof: A Sampling Profiler for Go](https://golangweekly.com/link/90876/rss) — [pprof](https://golangweekly.com/link/90877/rss) is the go-to tool for profiling Go code, but [it only profiles ‘on-CPU’ time](https://golangweekly.com/link/90878/rss). fgprof can work alongside pprof and measure ‘off-CPU’ time too (such as that spent on IO) so you can get the whole picture. [[Performance]]"
- [Profiling Go programs with pprof](https://jvns.ca/blog/2017/09/24/profiling-go-with-pprof/) by [[Julia Evans]] [[Profiling]] [[Performance]]
- [fuzz testing](https://readwise.io/reader/shared/01gma6d3mbc2xhz5bbz3638bhc)
- [github actions in go](https://readwise.io/reader/shared/01gnd5a2g8p3frh4m4cv2g19ms)
- see [escape analysis](https://go.dev/doc/gc-guide#Escape_analysis)
- [making a Go program run 1.7x faster with a one character change](https://hmarr.com/blog/go-allocation-hunting/)
- `go build gcflags=-m ./... | grep 'moved to heap'`
- [sourcegraph conc concurrency library](https://github.com/sourcegraph/conc)
- [design and motivation](https://readwise.io/reader/shared/01gs8z9a3e85krhm9sdtmjt0rg)
- [traefik mocktail mock generator](https://readwise.io/reader/shared/01gqv33cemvw3t7t3pppcvmmd4)
- [bithacking](https://lemire.me/blog/2023/02/07/bit-hacking-with-go-code/)
- [compiling C with AVX512 instructions to go asm](https://readwise.io/reader/shared/01gqv3tbzw11mbz84gdrhcfy1c)
- [gopher-lua](https://github.com/yuin/gopher-lua)
- embedded [[Lua]] VM and compiler
- thread local storage
- <https://github.com/jtolio/gls>
- <https://github.com/tylerstillwater/gls>
- <https://github.com/jwells131313/goethe>
- <https://github.com/timandy/routine>
![[Felix Geisendörfer#Go Profiler Notes]]
## go modules
- go modules [cheatsheet](x-devonthink-item://2E67D489-4379-4C02-B5A0-E7A8895CF2E0)
## go env
| command | notes |
| ------------------------------------------------------------------------- | ------------------------------------- |
| `go env` | active go environment |
| `go env GOENV` | path to persisted go environment file |
| `go env -w GOPROXY='binaries.ddbuild.io,https://proxy.golang.org,direct'` | set env var |
## devenv setup
```bash
go install github.com/x-motemen/gore/cmd/gore@latest
go install github.com/stamblerre/gocode@latest
go install golang.org/x/tools/cmd/godoc@latest
go install golang.org/x/tools/cmd/goimports@latest
go install golang.org/x/tools/cmd/gorename@latest
go install golang.org/x/tools/cmd/guru@latest
go install github.com/fatih/gomodifytags@latest
go install golang.org/x/tools/gopls@latest
go install github.com/cweill/gotests/gotests@latest
go install golang.org/x/tools/gopls@latest
go install golang.org/x/perf/cmd/benchstat@latest
go install github.com/felixge/pprofutils/v2/cmd/pprofutils@latest
```
## Runtime
- [Stacktraces in Go](x-devonthink-item://F7CEC868-8FD5-4203-913D-5221178DEB6F) by [[Felix Geisendörfer]]
- [Go Does Not Need A Java Style GC](https://readwise.io/reader/shared/01gng0z6ztddtp1yyxac6j39v5) ([devonlink](x-devonthink-item://C959487A-98B3-44B4-80BE-B67E0AE2509C))
- made partly in response to [[Mike Hearn]]'s critique of the Go GC in [Modern Garbage Collection](https://blog.plan99.net/modern-garbage-collection-911ef4f8bd8e) ([devonlink](x-devonthink-item://75563A56-E6E0-448A-8903-B2AC54AF74F2))
- [runtime.SetCgoTraceback](https://pkg.go.dev/runtime#SetCgoTraceback) for symbolizing C code
- https://github.com/ianlancetaylor/cgosymbolizer
- https://github.com/nsrip-dd/cgotraceback by [[Nick Ripley]]
- https://github.com/nsrip-dd/cgotraceback/issues/4
### Memory Tuning
- see [Go GC Guide](https://go.dev/doc/gc-guide)
- [soft memory limit](https://github.com/golang/proposal/blob/master/design/48409-soft-memory-limit.md) proposal, [discussion and acceptance](https://github.com/golang/go/issues/48409)
## Technique
^24ad9a
### Dataraces
- [Analysis of common go data races in Uber code base](https://readwise.io/reader/shared/01gndc3pw5qzw7k0ytsc2rzts4)
### use `-toolexec` flag to manipulate go AST at build time.
Allows for [[monkey patching]] the go runtime. ^d59016
This in combination with the [[#linkname directive]] can expose unexported state and functions to the toolexec tool
Sqreen Example
- https://blog.sqreen.com/dynamic-instrumentation-go/
- https://docs.sqreen.com/go/installation/#configure-the-go-toolchain-to-use-it
#### Alternatives
- https://github.com/felixge/go-patch-overlay
- patch the go runtime libraries at build-time
- [install go from source](https://go.dev/doc/install/source)
### `linkname` directive
from https://golang.org/cmd/compile/
Allows importing unexported variables and package-private state.
```
//go:linkname localname [importpath.name]
```
> This special directive does not apply to the Go code that follows it. Instead, the //go:linkname directive instructs the compiler to use “importpath.name” as the object file symbol name for the variable or function declared as “localname” in the source code. If the “importpath.name” argument is omitted, the directive uses the symbol's default object file symbol name and only has the effect of making the symbol accessible to other packages. Because this directive can subvert the type system and package modularity, it is only enabled in files that have imported "unsafe".
### pprof collection examples
```bash
curl -s http://localhost:6060/debug/pprof/heap > pprof.heap
curl -s http://localhost:6060/debug/pprof/profile > pprof.cpu
curl -s http://localhost:6060/debug/pprof/goroutine?debug=2 > pprof.goroutine
curl -s http://localhost:6060/debug/pprof/block > pprof.block
curl -s http://localhost:6060/debug/pprof/mutex > pprof.mutex
```
### slice tricks
- <https://github.com/golang/go/wiki/SliceTricks>
