Kitaab

Learning Go

dev go

published 2024-02-20 05:03

updated 2024-02-21 05:14

Functions

Functions are defined like so:

{{{go func main(x int, y int) int { / ... / } }}}

The type definition always comes after the name. If you have 2 parameters with the same type, you can omit all but the last:

func main(x int, y int) int -> func main(x, y int) int

Functions can also return any number of values:

func main(x, y int) (int, int)

Variables

Go uses var to define variables, with optional initializers

var x, y, z int = 1, 2, 3

Inside a function Go also allows the shorthand := to omit the var with implicit type: x := 42 Outside a function, all statements must begin with a keyword, so := will not work

The basic types in Go are:

{{{ 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 }}}

Uninitialized variables are given their type's zero value Type casting must be done explicitly in Go. const is the keyword for declaring constants (you cannot use :=)

Control Blocks

Go's only loop statement is for


Initialize

{{{bash go mod init example/user/hello cat go.mod

module example/user/hello

go 1.21.6 }}}

The first statement in a Go source file must be package name. Executable commands must always use package main.

You can install packages with go install and will put the executable in your GOBIN or GOPATH. go build will give you an executable in the local directory.

Importing

Go code is broken up into modules. You can create a module by creating a subdirectory in our a project:

{{{bash mkdir morestrings/ touch morestrings/reverse.go cat reverse.go

// Package morestrings implements additional functions to manipulate UTF-8 // encoded strings, beyond what is provided in the standard "strings" package. package morestrings

// ReverseRunes returns its argument string reversed rune-wise left to right. func ReverseRunes(s string) string { r := []rune(s) for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 { r[i], r[j] = r[j], r[i] } return string(r) } }}}

We can then import it in our main.go with import "exmaple/user/hello/morestrings

We can also import git repos directly with import "github.com/google/go-cmp/cmp" for example. We run go mod tidy to ensure we've locked the module version. The files all end up in GOPATH.

Testing

Testing is configured through creating a file ending in _test.go and containing functions named TestXXX with the signature func(t 'testing.T) (which will require an import "testing")

Tests can then be run with go test while in the directory.

Formatting

Thank god Go comes with a built in formatter. Just run go fmt

Names

Names are as important in Go as in any other language. They even have semantic effect: the visibility of a name outside a package is determined by whether its first character is upper case. By convention modules are lowercase, single word names.


I like text based tutorials, with a dev environment ready to try out For getting started with Nix + Go, check here

Some resources