Here you will find a collection of links, guides and examples to various topics related to Go programming.
- The Go Tour
- Effective Go
- Standard Library
- Go Playground
- Go Developer Roadmap.sh - Step by step guide to becoming a Go developer in 2023
- Validator - Package for validating Structs
A good (not the best as it depends on the situation) way to initialize a struct would be by implementing a "Constructor Method" with the required fields for the struct as arguments so that the consumer MUST pass them, and have everything else as optional parameters, which may be skipped.
Source: https://asankov.dev/blog/2022/01/29/different-ways-to-initialize-go-structs/
Expand
package people
// Properties are package privat
type Person struct {
age int
name string
}
type PersonOptions struct {
Age *int
}
func NewPerson(name string, options *PersonOptions) *Person {
p := &Person{name: name}
if options == nil {
return p
}
if options.Age != nil && options.Age != 0 {
p.age = *options.Age
}
return p
}
///////////////////////////////////////////////
package main
p := people.NewPerson("Anton", &people.PersonOptions{Age: 25})
// or
p := people.NewPerson("Anton", nil)
- JSON to Go
- Validator - Package for validating Structs (and therefor also unmarshaled JSON)
A good example how to use a multi-stage container image for Go application is the docker-compose-cli container image.
Important regarding performance is to use a cache for all RUN
commands which are using the Go binary. While go build
go will use the cache to not rebuild the whole application which increases the performance a lot!
RUN --mount=type=cache,target=/go/pkg/mod \
--mount=type=cache,target=/root/.cache/go-build \
go mod download
go install golang.org/x/vuln/cmd/govulncheck@latest
govulncheck ./...
Source: https://go.dev/blog/vuln
- ribice/glice - Shows a list with all dependencies and their licenses
Problem:
If you have a monorepo without using the go workspaces and you open this monorepo in vscode you will see errors in the code like this:
gopls was not able to find modules in your workspace.
When outside of GOPATH, gopls needs to know which modules you are working on.
You can fix this by opening your workspace to a folder inside a Go module, or
by using a go.work file to specify multiple modules.
See the documentation for more information on setting up your workspace:
https://github.com/golang/tools/blob/master/gopls/doc/workspace.md.go
Solution:
Those errors can mostly be resolved by adding the following experimental feature flag to your vscode settings:
{
"gopls": {
"experimentalWorkspaceModule": true
}
}
Copyright 2022-2023 cluetec GmbH and Contributors
The project is licensed under the Apache License 2.0.