This is an old revision of the document!
Table of Contents
GO
Go is a replacement of C, C++.
Go has some advantages when building simple, little services.
Simple language to build simle services.
So this language has API and syntax support for solving current prolems.
For microservices you need less polymorpism, but more concurency and verbosity.
- Build in concurrency. No mutex.
- Building for any platform
- Consistent API. Helpful when updating to new versions.
- One way to do things - less static code analysis needed
IDE: Gogland https://www.jetbrains.com/go/
There is a youtube channel about GO. HelloWorld, tooling,..
https://www.youtube.com/channel/UC_BzFbxG2za3bp5NRRRXJSw
Commands
Basic executable
package main func main() { println("hello") }
Build command
// in windows - builds a win application go build -o mywin.exe // or in ubuntu - builds a linux app go build -o myfirstexecutable
Run command
Build and executes
$ go run main.go hello
Docs of StandardLib
GO packages
for imports
GO modules loading
Loading a module and tidy up dependencies
$ go get github.com/jboursiquot/go-proverbs go mod tidy
GO private public functions
Capitized functions - are public.
small letter functions - are private.
//public func Fprintln(w io.Writer, a ...any) (n int, err error) { p := newPrinter() p.doPrintln(a) n, err = w.Write(p.buf) p.free() return } // private func newPrinter() *pp { p := ppFree.Get().(*pp) p.panicking = false p.erroring = false p.wrapErrs = false p.fmt.init(&p.buf) return p }
Naked return
Naked return. The name of the variable to return is defined in the method signature as “greeting”
// greetWithNameAndAge returns a greeting with the name and age func greetWithNameAndAge(name string, age int) (greeting string) { greeting = "Hello, my name is " + name + " and I am " + strconv.Itoa(age) + " years old." return }
Return multiple values
Ya can retrurn multiple value
func vals() (int, int) { return 3, 7 } func main() { a, b := vals() fmt.Println(a) fmt.Println(b) }
Return multiple value - with errors
The first value is returned, when there is no error.
func divide(a, b int) (int, error) { if b == 0 { return 0, errors.New("cannot divide by zero") } return a / b, nil }