kota's memex

tooling

go modules

go doc

go build

go testing

go benchmarking

go debugging

language

go constants

go if

go for

go statements

go functions

go printf

go slice

go maps

go enum

go set

go pointers

go struct

go interfaces

go concurrency

go generics

go log

go http

go html templates

go sql

go flags

go error

go sort

go json

vim-go

editor support and autoformatting

tidbits

cannot index *[]type

Pointers do not support array indexing, but you can just surround the pointer in parenthesis to tell go to return the pointer first:

var templateFlag *[]string
fmt.Println((*templateFlag)[i])

field alignment

It can actually make a big difference in terms of memory usage and it's impossible for the compiler to optimize (because of the ability to create structs without naming the fields; meaning the field order is part of the struct definition).

fieldalignment -fix .

reader line by line

Bufio's scanner is super useful for this exactly:

func main() {
    file, err := os.Open("/path/to/file.txt")
    if err != nil {
        log.Fatal(err)
    }
    defer file.Close()

    scanner := bufio.NewScanner(file)
    // optionally, resize scanner's capacity for lines over 64K, see next example
    for scanner.Scan() {
        fmt.Println(scanner.Text())
    }

    if err := scanner.Err(); err != nil {
        log.Fatal(err)
    }
}

sorting

type lengths []int

func (l lengths) Len() int {
    return len(l)
}

func (l lengths) Swap(i, j int) {
    l[i], l[j] = l[j], l[i]
}

func (l lengths) Less(i, j int) bool {
    return l[i] < l[j]
}

libraries

go tui

go ui

go web

go misc libraries

learn

The Go Programming Language - Alan A. A. Donovan

100 common go mistakes

https://100go.co/

https://lets-go.alexedwards.net/

Nice book on building web applications with go.

https://github.com/tmrts/go-patterns

Curated list of Go design patterns, recipes and idioms.

https://quii.gitbook.io/learn-go-with-tests/build-an-application/app-intro

Introduction to the language using a test-driven workflow.

https://kmcd.dev/posts/http0.9-from-scratch/

A tutorial and explanation of implementing http 0.9 from scratch. The information is important if you were to build your own custom tcp or udp protocol.

compilers

https://go.dev/

https://github.com/ziutek/emgo

https://github.com/tinygo-org/tinygo

https://go.dev/doc/install/gccgo