Skip to content

Om4ar/go-modules-play

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

golang intro!

reference

Installation

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

for vscode only ( install golang development dependencies )

install go extenstion

  1. run Go: Install/Update Tools command in vscode and install needed development dependencies
  2. go to settings and change Go: Format Tool to goimports

go modules for dependency management

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

video 1

video 2

go commands

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

examples of code working with go modules

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

profiling and benchmarking

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

  1. use the a benchmark tool to hit the api multiple times like go-wrk or other custom tool for 1 minute

  2. use pprof go tool pprof --seconds=5 localhost:3000/debug/pprof/profile

  3. use flame graphs => using go torch go torch -t 5 => this will output torch.svg diagram alternative go tool pprof -http=":8081" [binary] [profile] or ``go tool pprof -http=":8081" [binary] profile.cpu`

  4. 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 use go test -bench .

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages