Basics
Maximilian Wurzer
Advisory IT Architect, IBM Deutschland
Maximilian Wurzer
Advisory IT Architect, IBM Deutschland
Numbers
Integers (signed: int8
, unsigned: uint8
)
var myIntVar int = 23
Floating-Point Numbers
var myFloatVar float64 = 3.14
Strings
var myString string = "Hello World"
Booleans
var myBool bool = true
myNewBool := true
package main import "fmt" func main() { var myIntVar int = 23 var myFloatVar float64 = 3.14 var myString string = "Hello World" var myBool bool = true var myComplexVar complex128 = 5 + 3i fmt.Println("Integer:", myIntVar) fmt.Println("Float:", myFloatVar) fmt.Println("String:", myString) fmt.Println("My bool is", myBool) fmt.Println("My complex number is", myComplexVar) }
b := [5]int{1, 2, 3, 4, 5}
s := make([]string, 3)
m := make(map[string]int)
package main import "fmt" func main() { b := [5]int{1, 2, 3, 4, 5} fmt.Println("array:", b) s := make([]string, 2) fmt.Println("empty slice:", s) s[0] = "a" s[1] = "b" s = append(s, "e", "f") fmt.Println("slice:", s) m := make(map[string]int) m["k1"] = 7 m["k2"] = 13 fmt.Println("map:", m) fmt.Println("map entry:", m["k1"]) }
Take a look at Slice Tricks
6package main import "fmt" func zeroval(ival int) { ival = 0 } func zeroptr(iptr *int) { *iptr = 0 } func main() { i := 1 fmt.Println("initial:", i) zeroval(i) fmt.Println("zeroval:", i) zeroptr(&i) fmt.Println("zeroptr:", i, "| pointer:", &i) }
package main import "fmt" type person struct { name string age int } func newPerson(name string) *person { p := person{name: name} p.age = 42 return &p } func main() { fmt.Println(person{"Bob", 20}) fmt.Println(person{name: "Alice", age: 30}) fmt.Println(newPerson("Jon")) }
package main import "fmt" type geometry interface { area() float64 } type rect struct { width, height float64 } func (r rect) area() float64 { return r.width * r.height } func measure(g geometry) { fmt.Printf("Geo: %.2f | Area: %.2f", g, g.area()) } func main() { r := rect{width: 3, height: 4} measure(r) }
There is no public
, protected
or private
keyword.
Go does this by Naming-Conventions!
This applies to: Functions / Methods, Structs, Interfaces
Rules:
func MyPublicFunction() {}
type PublicStruct ...
func myPrivateFunction() {}
type privateStruct ...
if a := 5; 3 < a {}
package main import "fmt" func main() { var number = 5 if number < 0 { fmt.Println("Number is negative") } else if number > 0 { fmt.Println("Number is positive") } else { fmt.Println("Number is zero") } }
break
after cases needed.package main import "fmt" func main() { var mood = "🙂" switch mood { case "🙂", "😀": fmt.Println("I'm happy") case "😞": fmt.Println("I'm disappointed") case "😢": fmt.Println("I'm sad") default: fmt.Println("No mood") } }
package main import "fmt" func main() { var sum = 0 for i := 0; i <= 3; i++ { sum += i } for sum < 100 { sum += 1 } fmt.Println("Summe:", sum) // for { break } for index, value := range []string{"a", "b", "c", "d"} { fmt.Printf("Index: %d - Value: %s\n", index, value) } }
Functions which are executed concurrent are called goroutine
. These are defined as following (proc.go):
The main concepts are:
G - goroutine.
M - worker thread, or machine.
P - processor, a resource that is required to execute Go code.
M must have an associated P to execute Go code, however it can be
blocked or in a syscall w/o an associated P.
A goroutine
is assigned to a Thread which is assigned to a physical CPU.
package main import ( "fmt" "time" ) func f(from string) { for i := 0; i < 3; i++ { fmt.Println(from, ":", i) time.Sleep(time.Second) } } func main() { go f("goroutine") f("direct") fmt.Println("done") }
Channels
provide a way for two goroutines
to communicate with each other and synchronize their execution.
package main import "fmt" func f(from chan string) { for i := 0; i < 3; i++ { from <- fmt.Sprint(i) } } func main() { messages := make(chan string) go f(messages) msg := <-messages fmt.Println("First:", msg) msg = <-messages fmt.Println("Second:", msg) msg = <-messages fmt.Println("Third:", msg) }
(chan <-)
if there is a corresponding receive (<- chan)
readypackage main import "fmt" func main() { messages := make(chan string, 2) messages <- "buffered" messages <- "channel" fmt.Println(<-messages) fmt.Println(<-messages) }
Wait on multiple channel operations:
22A simple built-in interface to implement for custom Error-Types:
type error interface { Error() string }
Error Handling:
package main import "fmt" func myDiv(a, b int) (*int, error) { if b == 0 { var err error = fmt.Errorf("You cannot divide by zero!") return nil, err } else { var result int = a / b return &result, nil } } func main() { result, err := myDiv(5, 0) if err != nil { fmt.Println(err) } else { fmt.Printf("The result is %d", result) } }
GOGC
the garbage collector can be controlled (total heap size relative to reachable objects).github.com/golang/go/tree/master/src/cmd/compile
Go programs are compiled ahead of time to native machine code
One source - Many targets
GOOS=linux
- GOARCH=arm
GOOS=linux
- GOARCH=amd64
GOOS=linux
- GOARCH=ppc64le
GOOS=windows
- GOARCH=amd64
GOOS=darwin
- GOARCH=amd64
Go is statically compiled down to its target’s native machine and mostly outperforms its virtualized (e.g. Java, Kotlin) and interpreted (e.g. JavaScript) counterparts.
27gofmt
is an auto-formatterMaximilian Wurzer
Advisory IT Architect, IBM Deutschland