latest stable version 12.6
wget https://dl.google.com/go/go1.12.6.linux-amd64.tar.gz
extract and move
sudo tar -xvf go1.12.6.linux-amd64.tar.gz
sudo mv go /usr/local
setup golang evnironment variables
sudo nano ~/.bashrc
then add the next to access go binaries/tools and source code
# go binaries located
export GOROOT=/usr/local/go
# source code for go projects and downloaded packages
export GOPATH=$HOME/go
# go built code here
export GOBIN=$GOPATH/bin
# setting the path to access binaries in both go root and go path
export PATH="$HOME/bin:$HOME/.local/bin:$GOROOT/bin"
then load the .bashrc config
source .bashrc
install go
extenstion
- run
Go: Install/Update Tools
command in vscode and install needed development dependencies - go to
settings
and change Go: Format Tool togoimports
go modules is something like package.json for javascript projects , please read this for more info about go modules https://blog.golang.org/using-go-modules
https://github.com/golang/go/wiki/Modules
video tutorials
go mod init <NAME_OF_YOUR_MODULE>
=> start initializing the project as a go module, name of the module should be related to github repository path
go build
=> building the project will detect and download download used go packages then would build the go project from the folder
go mod edit --replace github.com/org/repo=../localFolderOfModule
=> command that would temporarly use the local developed module before it is committed
go get ./..
=> find all used modules and download them for go.mod without building the project
go mode why -m github.com/org/repo
=> would tell what is the tree usage of a module used from our go.mod
note: go mod
command downloads the need dependencies inside GOPATH/pkg
notice go.mod and how it is using replace to use the local folder for the go module, normally go module would fetched from vsc (github, ... ) and not from local folder like usage of modules in JavaScript for example
example: go modules in local development
example: working with when go.mod modules should commited
is to identify wich part of the code takes much time and cpu using any tool for benchmarking an api (making multiple requests on a route) then use a go tooling for to get results out
-
use the a benchmark tool to hit the api multiple times like go-wrk or other custom tool for 1 minute
-
use pprof
go tool pprof --seconds=5 localhost:3000/debug/pprof/profile
-
use flame graphs => using go torch
go torch -t 5
=> this will output torch.svg diagram alternativego tool pprof -http=":8081" [binary] [profile]
or ``go tool pprof -http=":8081" [binary] profile.cpu` -
how to write a benchmark (time per operations) for a single function instead of the whole project running using go-wrk or any other load tool make a
benchFuncName(b testing.B)
function for benchmarks that will loop over our method b.N watch for more info here then usego test -bench .