Posts

Showing posts with the label go

byte, int, rune in Golang

byte <=> uint8  rune <=> int32

Install Go for MacOS

===========install go==================  https://golang.org/doc/install Có 2 biến GOPATH và GOROOT cần lưu ý GOPATH là nơi dùng để lưu lại package hoặc file bin khi install một package nào đó, hoặc lưu trữ package download về từ git  GOROOT là nơi chứa thư viện mặc định của go khi cài đặt =============install visual code============ install plugin for golang install code in cmd https://code.visualstudio.com/docs/setup/mac =================go command=============== Usage: go <command> [arguments] The commands are: bug start a bug report build compile packages and dependencies clean remove object files and cached files doc show documentation for package or symbol env print Go environment information fix update packages to use new APIs fmt gofmt (reformat) package sources generate generate Go files by processing source get add dependencies to current module and i nstall them install compile and install pa...

Careful when comparing value with nil in Golang

func main () { err := Foo () fmt . Println ( err == nil ) } func Foo () error { var err * os . PathError = nil return err } /tmp/___go_build_main_go #gosetup false Process finished with exit code 0 Best practice func main () { err := Foo () fmt . Println ( err == nil ) } func Foo () error { var err * os . PathError = nil if err != nil { return errors . New ( "Not nil" ) } return nil } /tmp/___go_build_main_go #gosetup true Process finished with exit code 0

install golang

Open your terminal and navigate to your downloads folder cd /root/Downloads Extract the files tar -C /usr/local/ -xzf go1.13.6.linux-amd64.tar.gz Add variables for GO by modifying  “~/.bashrc” vim ~/.bashrc Add the following paths to the end of the file export GOPATH=/root/go-workspace export GOROOT=/usr/local/go PATH=$PATH:$GOROOT/bin/:$GOPATH/bin Now we need to refresh the bashrc to get the updated variables source ~/.bashrc

Carefully with go keyword in GOLANG

when you are using go keyword in Golang, it means that you are programming concurrency. So whatever you passed your variable to the concurrent function, the variable should be considered as a local variable of the function The below example is so popular and it can cause an unexpected error in your code func main () { slice := [] Model { { x : sql . NullString { String : "1" , Valid : true , }, y : 1 , z : "1" , }, { x : sql . NullString { String : "2" , Valid : true , }, y : 2 , z : "2" , }, { x : sql . NullString { String : "3" , Valid : true , }, y : 3 , z : "3" , }, } c := make ( chan int , 3 ) for i , v := range slice { log . Println ( i , v ) log . Printf ( "1---> %v \n ...

How to handle null data in SQL for struct in golang

type account struct { id int name string age sql . NullInt64 amt sql . NullFloat64 } func testReadNullvalue ( db * sqlx . DB ) { rows , _ := db . Query ( "select id, name, age , amount from account" ) for rows . Next () { var foo account rows . Scan (& foo . id , & foo . name , & foo . age , & foo . amt ) if foo . amt . Valid == true { fmt . Println ( "value: %v" , foo . amt . Float64 ) } else { fmt . Println ( "data is null, print with default value " , foo . amt . Float64 ) } fmt . Println ( "%+v" , foo ) } }

Go module

go run -mod=vendor  main.go -c=config.yml The above cmd will run app with the vendor libs that downloaded go mod vendor The cmd will create vendor lib folder in vendor folder go mod init  This will generate file go.mod