[comment]: # "markdown: { smartypants: true }" # Go Just Works™ The Philosophy, Language, and Tooling of Go
(An Intro to Go for People Who Love Stuff that Just Works, Everywhere)
### Useful Links
Slides:
beyondcodebootcamp.github.io
Resources for Learning Go:
github.com/BeyondCodeBootcamp/beyondcodebootcamp/issues/19
Video:
https://youtu.be/__COMING_SOON__
"Hello Go" (2022)
youtube.com/watch?v=NtKBcVku6EU
Go Proverbs (Poster):
github.com/BeyondCodeBootcamp/go-proverbs
Template Repo:
github.com/BeyondCodeBootcamp/hello-go
Slides Builder:
MD Slides
## Go Just Works™
The Philosophy, Language,
and Tooling of Go
## AJ ONeal
[@\_beyondcode](https://twitter.com/@_beyondcode)
[youtube.com/coolaj86](https://youtube.com/coolaj86)
## Dangerous Wrong Thinker
Equal Opportunity Offender
Technophobic Technologist Extraordinairé
Useful? Entertaining? 👍 🔔 ➡️
## Part 1: The Philosophy of Go (is it for you?)
> Go is for web servers - > [Rob Pike](https://www.infoq.com/presentations/Go-Google/)
- [Go Programming](https://www.youtube.com/watch?v=jgVhBThJdXc) (How to Think in Go) (2010) - [Effective Go](https://go.dev/doc/effective_go) (55 pages) - [Go at Google](https://www.infoq.com/presentations/Go-Google/) (2012) - [The Go Proverbs](https://www.youtube.com/watch?v=PAAkCSZUG1c) (2015) - [Simplicity is Complicated](https://www.youtube.com/watch?v=rFejpH_tAHM) (2015) - [Towards Go 2](https://go.dev/blog/toward-go2) (2017) - [Go with Versions](https://www.youtube.com/watch?v=F8nrpe0XWRg) (2018) - [Practical Go](https://www.youtube.com/watch?v=gi7t6Pl9rxE) (2019) - [Hello Go](https://beyondcodebootcamp.github.io/presos/hello-go/) (2022) - [Go: What We Got Right & Wrong](https://www.youtube.com/watch?v=yE5Tpp2BSGw) (2023) - [And more...](https://github.com/BeyondCodeBootcamp/beyondcodebootcamp/issues/19)
> A little copying is better than a little dependency.
> A language you can learn in a weekend.
### Small Language 25 Keywords & ~50 Built-ins - https://go.dev/ref/spec#Keywords - https://pkg.go.dev/builtin
### Big Standard Library Batteries Included - https://pkg.go.dev/std - https://pkg.go.dev/golang.org/x
### No Stupid Questions ```sh go fmt ./... ```
### No Clever Answers
### Slow & Steady > We did what we always do when there’s a problem without a > clear solution: **_we waited_**.
Russ Cox,
Toward Go 2
> Waiting gives us more time to add **_experience and > understanding_** of the problem and also more time to find > a good solution.
#### Rob Pike's Reflections, 14 Years Later
What We Got Right, What We Got Wrong (2023) -
youtube.com/watch?v=yE5Tpp2BSGw
- Strong Standard Library - Concurrency is First-Class - Composition > Inheritance - Packages that Minimize Dependency - Fast Build & Testing - Consistent Formatting - Readability > Cleverness - Compatibility Guarantee
## Part 2: The Language
- [A Tour of Go](https://go.dev/tour/welcome/1) - [Effective Go](https://go.dev/doc/effective_go) (55 pages) - [Go by Example](https://gobyexample.com/) - [yourbasic.org/golang](https://yourbasic.org/golang) - [Practical Go](https://www.youtube.com/watch?v=gi7t6Pl9rxE) (2019) - [How I Write HTTP Web Services after 8 Years](https://www.youtube.com/watch?v=rWBSMsLG8po) (2019)
> If you're going to write a Python program, you wouldn't > start with a C++ program and translate it line-by-line and > expect to end up with a very well-written Python > program. - > [Russ Cox](https://youtube.com/watch?v=jgVhBThJdXc&t=66)
Throw away your CS degree.
- One way - No inheritance - No circular dependencies
### Static, Self-Contained Install Friends don't let friends `apt-get`: ```sh curl https://webi.sh/go | sh source ~/.config/envman/PATH.env ``` ```sh ~/.local/opt/go/bin => ~/.local/opt/go-v1.22.1/ ~/go/bin => ~/.local/opt/go-bin-v1.22.1/ ```
### Package The Fundamental Unit
> Design the architecture, name the components, document the > details.
It's about the _function_, not the _category_ ```sh go mod init 'github.com/acme/router' ``` ```sh go.mod ```
What you **won't** see ```sh go mod init 'github.com/acme/arrayutils' ```
Explicit ancestry. No guessing. ```go import ( "github.com/acme/foobar/config" awsconfig "github.com/aws/aws-sdk-go-v2/config" awsiot "github.com/aws/aws-sdk-go-v2/service/iot" ) ```
### Interface The Fundamental Type
> The bigger the interface, the weaker the abstraction. (the ideal interface is a single method)
- shields against dependencies - provides "generics"
- if it's public, & does stuff, make an interface - avoid depending on interfaces
Doers only, no values ```go type FooReadWriter interface { Read(p []byte) (n int, err error) Write(p []byte) (n int, err error) } ```
### Concurrency The Fundamental Concept
- Goroutines - Channels - Mutexes
> Concurrency is not parallelism. > > Channels orchestrate; mutexes serialize.
> await by default
```go done := chan struct{}{} go doStuff(done) <- done ```
## 8. Hello `go generate`
`generate.go`: ```go //go:generate stringer -type=Greeting package hello ```
❌ `Makefile`
❌ `Makefile` - ❌ Cross-platform
❌ `Makefile` - ❌ Cross-platform - 🤮 gcc toolchain
`generate.go`: ```go //go:generate go run -mod=vendor golang.org/x/tools/cmd/stringer -type=Greeting package hello ```
## 10. Hello `./vendor/`
- PREDICTABLE
- PREDICTABLE - REPRODUCIBLE
- PREDICTABLE - REPRODUCIBLE - GUARANTEED
```bash [1-2] go mod tidy go mod vendor ```
```bash [1-2] go generate -mod=vendor ./... go build -mod=vendor ./... ```
```txt -mod=vendor ```
`.ignore`: ```txt vendor ```
## 13. Hello `godoc`
`doc.go`:
[godoc-tricks](https://pkg.go.dev/github.com/fluhus/godoc-tricks)
## 14. Hello `version`
```go git tag v1.0.0 ```
- GoReleaser - Go GitVer: \
```go var ( version = "v0.0.0" date = "0001-01-01T00:00:00Z" commit = "0000000" ) ```
```go import ( "runtime/debug" ) function main() { build := debug.ReadBuildInfo() fmt.Println( build.Main.Version, build.Main.Sum, ) } ```
```txt -V --version version -version ```
```go if len(args) > 1 { arg := args[0] if "-V" == arg || "version" == strings.TrimLeft(arg, "-") { ver() os.Exit(0) } } ```
for `help` too
```go if len(args) > 1 { arg := args[0] if "-h" == arg || "help" == strings.TrimLeft(arg, "-") { ver() os.Exit(0) } } ```
## 15. Hello `embed`
``` # Create config directory hello init # Serve auto-magic configurator hello server ```
```go package hello import "embed" //go:embed html/* assets/* // FS is the virtual embedded filesystem for all the Hello World things var FS embed.FS ```
```go import ( // ... hello "github.com/BeyondCodeBootcamp/hello-go" ) func main() { f, _ := hello.FS.Open("assets/proverbs.txt") proverbs, _ := ioutil.ReadAll(f) fmt.Printf("%s", proverbs) } ```
## 16. Hello `goreleaser`
Cheat Sheet: https://webinstall.dev/goreleaser
- PREDICTABLE
- PREDICTABLE - REPRODUCIBLE
- PREDICTABLE - REPRODUCIBLE - GUARANTEED
`.goreleaser.yml`: ```yml builds: - env: - CGO_ENABLED=0 main: ./main.go binary: mssql-to-csv goos: - linux - windows goarch: - amd64 - arm64 archives: - format_overrides: - goos: windows format: zip ```
```txt ~/.config/goreleaser/github_token ```
- version - git commit - date
`main.go`: ```go var ( version = "v0.0.0" date = "0001-01-01T00:00:00Z" commit = "0000000" ) ```
Cheat Sheet: https://webinstall.dev/goreleaser
Q&A
Thanks.
Like, Sub, & Follow
(if you wannu)
FIN