programming:go
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revision | |||
programming:go [2023/11/01 07:31] – removed - external edit (Unknown date) 127.0.0.1 | programming:go [2023/11/01 07:31] (current) – ↷ Page moved from camunda:programming:go to programming:go skipidar | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ===== 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, | ||
+ | - 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:// | ||
+ | |||
+ | There is a youtube channel about GO. HelloWorld, tooling,.. \\ | ||
+ | https:// | ||
+ | |||
+ | ==== Commands ==== | ||
+ | |||
+ | Basic executable | ||
+ | <sxh go> | ||
+ | package main | ||
+ | |||
+ | func main() { | ||
+ | println(" | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ==Build command== | ||
+ | |||
+ | <sxh go> | ||
+ | // 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 | ||
+ | |||
+ | <sxh go> | ||
+ | $ go run main.go | ||
+ | hello | ||
+ | </ | ||
+ | |||
+ | |||
+ | ==Docs of StandardLib== | ||
+ | https:// | ||
+ | |||
+ | ==GO packages== | ||
+ | |||
+ | |||
+ | for imports | ||
+ | |||
+ | https:// | ||
+ | |||
+ | ==GO modules loading== | ||
+ | |||
+ | Loading a module and tidy up dependencies | ||
+ | |||
+ | <sxh go> | ||
+ | $ go get github.com/ | ||
+ | go mod tidy | ||
+ | </ | ||
+ | |||
+ | |||
+ | ==GO private public functions== | ||
+ | |||
+ | Capitized functions - are public. | ||
+ | |||
+ | small letter functions - are private. | ||
+ | |||
+ | <sxh go> | ||
+ | |||
+ | //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(& | ||
+ | return p | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | ==Naked return== | ||
+ | |||
+ | Naked return. The name of the variable to return is defined in the method signature as " | ||
+ | |||
+ | <sxh go> | ||
+ | |||
+ | // greetWithNameAndAge returns a greeting with the name and age | ||
+ | func greetWithNameAndAge(name string, age int) (greeting string) { | ||
+ | greeting = " | ||
+ | return | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | == Return multiple values == | ||
+ | |||
+ | Ya can retrurn multiple value | ||
+ | |||
+ | <sxh go> | ||
+ | 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. | ||
+ | |||
+ | |||
+ | <sxh go> | ||
+ | func divide(a, b int) (int, error) { | ||
+ | if b == 0 { | ||
+ | return 0, errors.New(" | ||
+ | } | ||
+ | return a / b, nil | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | == Variables == | ||
+ | |||
+ | The ''': | ||
+ | |||
+ | |||
+ | <sxh go> | ||
+ | |||
+ | package main | ||
+ | |||
+ | import ( | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | ) | ||
+ | |||
+ | // package variables | ||
+ | var x, y, z bool = true, true, true | ||
+ | |||
+ | // accesses the package level values | ||
+ | func packlvlvars() (bool, bool, bool) { | ||
+ | return x, y, z | ||
+ | } | ||
+ | |||
+ | func main() { | ||
+ | |||
+ | // method variables | ||
+ | // below vars are covering the package-level values in the scope of current function | ||
+ | |||
+ | |||
+ | // long variant | ||
+ | // split declaration and assignment | ||
+ | var mvar int | ||
+ | mvar = 123 | ||
+ | fmt.Println(mvar) | ||
+ | |||
+ | |||
+ | // combine declaration and assignment | ||
+ | var mvar2 int = 123 | ||
+ | fmt.Println(mvar2) | ||
+ | |||
+ | |||
+ | // declare multiple | ||
+ | x, y, z := 1, 2, " | ||
+ | fmt.Println(strconv.Itoa(x) + strconv.Itoa(y) + z) | ||
+ | |||
+ | // need a special value to get package variables | ||
+ | a, b, c := packlvlvars() | ||
+ | fmt.Println(strconv.FormatBool(a) + strconv.FormatBool(b) + strconv.FormatBool(c)) | ||
+ | |||
+ | } | ||
+ | |||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | == Pointers == | ||
+ | |||
+ | |||
+ | <sxh go> | ||
+ | // basic-types/ | ||
+ | package main | ||
+ | |||
+ | import ( | ||
+ | " | ||
+ | " | ||
+ | ) | ||
+ | |||
+ | func main() { | ||
+ | // create a variable of type *T where T is an int | ||
+ | var a *int | ||
+ | |||
+ | // declare and assign `b` variable of type int | ||
+ | valu := 100 | ||
+ | |||
+ | // assign the address of b to a | ||
+ | pointr = &valu | ||
+ | |||
+ | // print out the value of a which is the address of b | ||
+ | fmt.Println(pointr) | ||
+ | // returns: ptr | ||
+ | fmt.Println(" | ||
+ | |||
+ | // print out the value at the address of b | ||
+ | fmt.Println(*pointr) | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | == Structs & pass by reference or value == | ||
+ | |||
+ | Structure to hold data in Go Lang | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | <sxh go> | ||
+ | type author struct { | ||
+ | first string | ||
+ | last string | ||
+ | } | ||
+ | |||
+ | // getter | ||
+ | // attention - the assignment to struct - is derived from parenthesis after " | ||
+ | // is declared OUTSIDE of struct :( | ||
+ | func (a author) fullName() string { | ||
+ | return a.first + " " + a.last | ||
+ | } | ||
+ | |||
+ | // setter | ||
+ | // passing the reference, so that we change the exact object and not its value | ||
+ | // changeName changes the first and last name of the author | ||
+ | func (a *author) changeName(first, | ||
+ | a.first = first | ||
+ | a.last = last | ||
+ | } | ||
+ | |||
+ | func main() { | ||
+ | // initialize author | ||
+ | a := author{ | ||
+ | first: " | ||
+ | last: | ||
+ | } | ||
+ | |||
+ | // print the author' | ||
+ | fmt.Println(a.fullName()) | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | '' | ||
+ | |||
+ | Explains why to pass | ||
+ | |||
+ | {{https:// | ||
+ | |||