GoLang

Guide to setup golang development environment on MacOS without admin privileges

You can use either JetBrains GoLand or VSCode as your go IDE.

Recommended tools/libs for Go projects:

Tools

  1. GoReleaser - Cross-compile and Release to GitHub

  2. protobuf - gRPC code gen tool and serialization library

  3. ko - Build, sign and publish OCI images from source code.

Libraries

  1. testify - Unit and integration testing

  2. mockery - generate mocks for golang interfaces

  3. entgo - An entity framework for Go

Install

brew install go
# verify 
go version                                                                                                                                                                           rancher-desktop on ☁️  
# go version go1.21.1 darwin/arm64

Multiple go versions

If you need multiple versions for testing...

Optional tools for GoLang Developers

Make sure you have following in your ~/my/paths.zsh

for complete project example with VSCode settings, refer hello project

Workspace

~/go/bin, ~/go/pkg, ~/go/src are created automatically when you pull your first dependency.

Project

Your first project

create a project directory outside your GOPATH:

Initialize module

this will create go.mod file

Write your code

Build and Run

Note: The go.mod file was updated to include explicit versions for your dependencies In contrast, go build and go test will not remove dependencies from go.mod that are no longer required and only update go.mod based on the current build invocation's tags/OS/architecture.

Test

Install

this will copy binary into $GOPATH/bin i.e., ~/go/bin

Release

recomdned steps for releasing modules

Ensure your go.sum file is committed along with your go.mod file.

A new module version may be published by pushing a tag to the repository that contains the module source code.

Docs

Format

format your source code with prettier or fmt

Daily Workflow

Note there was no go get required in the example above.

Your typical day-to-day workflow can be:

  • Add import statements to your .go code as needed.

  • Standard commands like go build or go test will automatically add new dependencies as needed to satisfy imports (updating go.mod and downloading the new dependencies).

  • When needed, more specific versions of dependencies can be chosen with commands such as go get foo@v1.2.3, go get foo@master, go get foo@e3702bed2, or by editing go.mod directly.


Go Commands

Points

  • A repository contains one or more Go modules.

  • Each module contains one or more Go packages.

  • Each package consists of one or more Go source files in a single directory.

  • A module is defined by a tree of Go source files with a go.mod file in the tree's root directory.

  • The import paths for all packages in a module share the module path as a common prefix.

  • The module path and the relative path from the go.mod to a package's directory together determine a package's import path.


FAQ

What go build do?

What the go command does depends on whether we run it for a "normal" package or for the special "main" package.

For packages

  • go build builds your package then discards the results.

  • go install builds then installs the package in your $GOPATH/pkg directory.

For commands (package main)

  • go build builds the command and leaves the result in the current working directory.

  • go install builds the command in a temporary directory then moves it to $GOPATH/bin.

Basically you can use go build as a check that the packages can be built (along with their dependencies) while go install also (permanently) installs the results in the proper folders of your $GOPATH.

It is also worth noting that starting with Go 1.5 go install also removes executables created by go build

How to cross Compiling on MacOS For Linux runtime?


Learning

  1. Data Journey with Golang, GraphQL and Microservices. Ref: YouTube talks about BigTable --> Data Microservices (Go-Micro) --> GraphQL Gateway-gqlgen (ACL check permission at field level, codegen)

  2. Interfaces , genarics (concrete types vs abstract types). Ref: YouTube Lessons Learned: "Always Return Concrete types, receive interfaces as perameters"

  3. Heap vs Stack memory. Ref: YouTube Are pointers a performance optimization? Ref: medium


Reference

Last updated

Was this helpful?