Took part in a Go workshop by Chuk organised by GeeksHacking, where we made a command line tool that interacts with the Boardgame Atlas API to search for board games.
Go
Languages can be thought of in terms of STRONG vs WEAK typing, and static v dynamic
Where Go is used
- Infra like Kubernetes and Docker
- Cloud native devt (writing RESTful [message-based] web apps; gRPC (another service style))
- Replacing existing infra such as CoreDNS/Caddy (reverse proxy - nginx is an alternative to Caddy. Chuk likes Caddy bc it just has one binary to deal with)
Go Data Types
- string
- rune (equivalent of a char in JS)
- number int, uint (no negative numbers), floats
- complex
var name string
var age uint8 = 21
Arrays & Slices (dynamic arrays)
Arrays (fixed size)
var name [5]string
Slices
Looks like you declare the upper limit of memory it can have (e.g. 5 items) and then append items without necessary using all of the memory allocated
TODO: look up arrays vs Slices
Loops
Pointers
Python etc have a concept of a reference - a pointer-like thing to an object
- in Java, objects arrays etc are references but primitives are not references.
Chuk: “You wont ask an F1 driver to ask slowly right. Sometimes you want the freedom to do stupid things [as an engineer].”
name := "fred"
&name #gives you the address of a var
*&name == "fred" # gives you contents of the address
This allows you to have double pointers that allow “dynamic addressing” (TODO: look up)
And anything can be passed by reference or value into functions
Creating custom types
type natural unit32
or create objects
type Customer struct {
...
}
Function types
type Apply[T any] func(T,T)T
# function takes 2 params of type T, and returns type T.
Visibility of vars
If a var starts with uppercase it is public, and lowercase is private.
Chuk: “You either see it or you don’t”
Class-like
Can write functions that are only accessible by certain structs
Receiver function must be in the same module
Critique of Go: You extend or implement an interface, but there’s no explicit stuff like that in Go. Go handles it implicitly where if your struct has methods of the interface, it is considered implemented.
This makes reading code a bit difficult because a param can require a reader but you have to figure out which structs implement a reader, then hunt down the methods in that reader.
TODO: Find out what’s a thread … Chuk is talking about threads but I don’t know what it’s about
Communicating between routines
Use a built-in primitive called channels.
To read:
Today’s workshop
Building a command line tool to search for board games
If there’s time:
- Pretty print results
- Cross compile to other platforms e.g. Linux to Windows
- Write a multi threaded version if time permits
Initialise project as a Go project
Run go mod init
in the directory where your project is. It should create a file called go.mod
.
My first Go program :)
package main
func main() {
fmt.Println("hello, world")
}
Run programs with go run .
in the directory where your go program file is stored.
Boardgame API
We are using the Search endpoint: https://www.boardgameatlas.com/api/docs/search
We need:
- Search term
- Client id
- Limit (e.g. 10)
- Skip (e.g. 0)
Compiling
Use go build .
go run
is for devt purposes.
Questions
What are pointers and references? What does strong and weak typing mean, and when would someone prefer a language that’s strongly/weakly typed?
Adding colors:
Cool repo to add colors to the command line output: https://github.com/fatih/color
run go get github.com/fatih/color
in your project dir