Coder Social home page Coder Social logo

go-guide's Introduction

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.

go-guide's People

Contributors

a-mesin avatar florianrusch avatar

Stargazers

 avatar Florian Rusch (ZF Friedrichshafen AG) avatar  avatar

Watchers

Iskandar Abudiab avatar René Albers avatar N4tht4N avatar  avatar  avatar

go-guide's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.