why golang
go has concurrency/mutexs, is faster, has an idomatic way of writing go. a unique thing about go is that it return error does not throws error. code is verbose but motivation is u are learning something new
- compiles single binary
- staticly typed
- performamce
- no need for a web framework (go standard libary has everything)
basics
go has modules and packages modules are the entire project that you start with go mod init github.com/username/packagname
every go file starts with package name, and every go file that needs to be converted to an executable needs to have a main package with main func in it package main
go supports UTF-8 encoding so u can use chinese also
you can run the program using go run main.go it saves the results and caches so it is faster than go build
you can also compile the file and run the executable, where u can also compile the for other os set GOOS=linux set GOARCH=amd64 go build -o linux-app
- package names has to be short as possible and lowercase
Encapsulation in go all variables of the package which are in small letter (math.pi) are private to package only whereas if it is accessible to other package it should be captial (math.Pi)
- Using the data types
uint8
pointers points to virtual address as we are abstracted by the go runtime
zero value of an refernce types are
nil
Goroutines
the main function is executed main goroutine is created
the goroutines are managed by go scheduler
goroutines(2kb - 4kb) are very light weight compared to threads which are around 265kb - 1mb also they can be resized
concurrency -> in javascript (event loop) example 4 tasks can get executed in any order ( most effient way)
parrallesism -> depends on hardware (cpu core) here all the task are executed parallel
goroutine requries main goroutine to run
csp -> communicating sequential processes