programming:go
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| programming:go [2023/09/04 15:56] – [Commands] skipidar | programming:go [2023/11/01 07:31] (current) – ↷ Page moved from camunda:programming:go to programming:go skipidar | ||
|---|---|---|---|
| Line 151: | Line 151: | ||
| == Variables == | == Variables == | ||
| + | |||
| + | The ''': | ||
| + | |||
| <sxh go> | <sxh go> | ||
| Line 162: | Line 165: | ||
| ) | ) | ||
| + | // package variables | ||
| var x, y, z bool = true, true, true | var x, y, z bool = true, true, true | ||
| Line 171: | Line 175: | ||
| func main() { | func main() { | ||
| + | // method variables | ||
| // below vars are covering the package-level values in the scope of current function | // 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, " | x, y, z := 1, 2, " | ||
| fmt.Println(strconv.Itoa(x) + strconv.Itoa(y) + z) | fmt.Println(strconv.Itoa(x) + strconv.Itoa(y) + z) | ||
| Line 184: | Line 203: | ||
| </ | </ | ||
| + | |||
| + | |||
| + | == 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:// | ||
| + | |||
programming/go.1693842998.txt.gz · Last modified: by skipidar
