Skip to content

Collection of guides and links for people who want to develop in Go

License

Notifications You must be signed in to change notification settings

cluetec/go-guide

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 

Repository files navigation

Go Guide

Here you will find a collection of links, guides and examples to various topics related to Go programming.

Getting Started

Installation

Best practices

Slices

Structs

Initialization

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 un-/marshaling

Concurrency

Tests

Memory optimization

Orchestration

Dockerfile

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

Vulnerability Management

go install golang.org/x/vuln/cmd/govulncheck@latest
govulncheck ./...

Source: https://go.dev/blog/vuln

Licensing

  • ribice/glice - Shows a list with all dependencies and their licenses

Examples

Blogs

IDEs / Editor

Visual-Studio Code (vscode)

Open monorepo without go.work file

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

Further links

License

Copyright 2022-2023 cluetec GmbH and Contributors

The project is licensed under the Apache License 2.0.