In 2023, I became interested in the programming language Go. It is a C derivates without all the nil pointer exceptions, and has a very solid infrastructure and libraries to compile across different operating systems.
Nevertheless, it has some quirks which I would like to keep here as a fast reference and cheat sheet.
Date formats
This is my opinion is the worse design decision they have taken. Formatting times and dates is not done using letters, but numbers.
A good summary is given in Go Samples, here as a quick sheet:
01
- month02
- day03
- hour (12h)04
- minute05
- second06
- year07
- time zone offsetJanuary
- month as long textJan
- month as short textMonday
- weekday as long textMon
- weekday as short text
Some standard layouts are useful (taken from Go Samples)
const (
// YYYY-MM-DD: 2022-03-23
YYYYMMDD = "2006-01-02"
// 24h hh:mm:ss: 14:23:20
HHMMSS24h = "15:04:05"
// 12h hh:mm:ss: 2:23:20 PM
HHMMSS12h = "3:04:05 PM"
// text date: March 23, 2022
TextDate = "January 2, 2006"
// text date with weekday: Wednesday, March 23, 2022
TextDateWithWeekday = "Monday, January 2, 2006"
// abbreviated text date: Mar 23 Wed
AbbrTextDate = "Jan 2 Mon"
)
Enum types
Go does not support enum types, but they can be simulated. Again, take a look at Gosamples enum for a good overview.
type Season int
const (
Spring Season = iota + 1
Summer
Autumn
Winter
)
The iota
indicator (I have no clue where this word is coming from) just numbers the constant values 1, 2, 3, …
Note that when such an enum is marshalled, you have to provide custom marshaller and demarshaller.
Generics
Generics are parameterized types in Go. Some background can be found in the tutorial.
Embedding
Files can be embedded in the generated binary. See <>.
import embed
//go:embed: myfiles
res embed.FS
If you use file embedding with templates, proceed as follows to embed files in the sub-folder templates
:
import (
"embed"
"html/templates"
"github.com/gin-gonic/gin"
)
var (
//go:embed templates/*
templFolder embed.FS
tmplFiles = template.Must(template.ParseFS(templFolder, "templates/index.html", "templates/jobinfo.html", "templates/jobinfo.md"))
)
func main() {
router := gin.Default()
router.SetHTMLTemplate(tmplFiles)
router.GET("/index", func (c *gin.Context) {
c.HTML(http.StatusOK, "index.html", gin.H{
"title": "My title",
})
router.Run()
}
Code generation
Code can be generated during a go build. See Go generate.
//go:generate goyacc -o gopher.go -p parser gopher.y
Swagger
Swagger is a standardized way to document REST APIs. In Go is is supported by swaggo/swag. A good documentation and example can be found at LogRocket.
swaggo/swag supports either generating Go code from swagger specifications, or annotating Go code with special documentation tags and generate the Swagger interface from the code.
Functional language
Go is not a functional language. Some attempts have been made to bring in some functional concepts into it:
- Github rp-go: I found this one promising and well explained
- Functional Programming with Go-Lang: https://pkg.go.dev/github.com/BooleanCat/go-functional
Various
https://marketplace.visualstudio.com/items?itemName=golang.Go
How to bring it to IIS: https://labod.co/posts/running_a_go_project_with_iis
Article on AI word embeddings: https://cybernetist.com/2024/01/07/fun-with-embeddings/, using gocolly for web scraping and go-echarts for visualization.