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
GoReleaser - Cross-compile and Release to GitHub
protobuf - gRPC code gen tool and serialization library
ko - Build, sign and publish OCI images from source code.
Libraries
testify - Unit and integration testing
mockery - generate mocks for golang interfaces
entgo - An entity framework for Go
Install
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
andgo test
will not remove dependencies fromgo.mod
that are no longer required and only updatego.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
orfmt
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
orgo test
will automatically add new dependencies as needed to satisfy imports (updatinggo.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?
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
Signup for https://golangweekly.com/
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)
Interfaces , genarics (concrete types vs abstract types). Ref: YouTube Lessons Learned: "Always Return Concrete types, receive interfaces as perameters"
Reference
goreleaser supply-chain-example
Last updated