May include word games. Let's Go!
package main
import "fmt"
func main() {
fmt.Println("Hello World!")
}
go run app.go // run the code in dev mode
go build app.go // compiling
./app.go // executing the build-file
// Providing the value later
var name string
name = "Max"
// Providing the value fist
var name string = "Max"
// Using type inference
name := "Max"
var width, height float32 = 2.5, 6.1
// or:
width, height := 2.5, 6.1
fmt.Println(width) // 2.5
fmt.Println(height) // 6.1
var firstVar bool
fmt.Println(firstVar) // false
var firstVar string
fmt.Println(firstVar) // ""
var firstVar float64
fmt.Println(firstVar) // 0
var firstVar int
fmt.Println(firstVar) // 0
func add(x int, y int) int {
return x + y
}
func main() {
fmt.Println(add(5, 5)) // 10
}
func add(x, y int) int {
return x + y
}
func person(name string, age int) (string, int) {
return name, age + 1
}
func main() {
name, age := person("max", 22)
fmt.Println(name, age)
// max 23
}
var ages [5]int
fmt.Println(ages)
// [0 0 0 0 0]
var ages [5]int
ages[0] = 21
ages[1] = 23
fmt.Println(ages)
// [21 23 0 0 0]
// the better way:
ages := [5]int{23, 21, 19, 23, 24}
fmt.Println(ages)
// [23 21 19 23 24]
ages := []int{23, 21, 19, 23, 24}
ages = append(ages, 27)
// append does not edit the existing slice.
// It returns a new one.
fmt.Println(ages)
// [23 21 19 23 24 27]
ages := [3]int{23, 21, 19}
for index, value :=range ages {
fmt.Println(index, value)
// 0 23
// 1 21
// 2 19
}
if 5 > 3 {
fmt.Println("5 > 3")
} else {
fmt.Println("3 > 5")
}
if 5 > 5 {
fmt.Println("5 > 3")
} else if 5 >= 5 {
fmt.Println("Greater / equal")
} else {
fmt.Println("3 > 5")
}
if age := 9; age < 18 {
fmt.Print("Not grown up")
}
func main() {
for i := 0; i < 5; i++ {
fmt.Println(i)
}
}
for i := 0; i < 5; {
fmt.Println(i)
// endless prints
}
func main() {
var age = 24
var agePointer = &age
fmt.Println(agePointer)
// for example '0xc000016078'
}
var age = 24
var agePointer = &age
fmt.Println(*agePointer) // 24
var age = 24
var agePointer = &age
*agePointer++
fmt.Println(*agePointer) // 25
type person struct {
name string
age int
}
func main() {
personMax := person{name: "Max", age: 21}
fmt.Println(personMax) // { Max 21}
fmt.Println(personMax.name) // Max
}
people := make(map[string]int)
people["Max"] = 23
people["Tom"] = 19
fmt.Println(people["Max"])
people := make(map[string]int)
people["Max"] = 23
people["Tom"] = 19
for key, value := range people {
fmt.Println(key, value)
// Max 23
// Tom 19
}