From de9137b84efcb03c84104b3b40d90f25ba9b8ce6 Mon Sep 17 00:00:00 2001 From: Jonas Date: Sun, 1 Mar 2026 21:03:32 +0100 Subject: [PATCH] feat: init & playaround --- go-by-example/arrays/arrays.go | 38 ++++++++++++++++++ go-by-example/constants/constants.go | 22 +++++++++++ go-by-example/for/for.go | 35 +++++++++++++++++ go-by-example/functions/functions.go | 33 ++++++++++++++++ go-by-example/hello-world/hello-world.go | 7 ++++ go-by-example/maps/maps.go | 42 ++++++++++++++++++++ go-by-example/slices/slices.go | 48 +++++++++++++++++++++++ go-by-example/switch/switch.go | 49 ++++++++++++++++++++++++ go-by-example/values/values.go | 17 ++++++++ go-by-example/variables/variables.go | 22 +++++++++++ 10 files changed, 313 insertions(+) create mode 100644 go-by-example/arrays/arrays.go create mode 100644 go-by-example/constants/constants.go create mode 100644 go-by-example/for/for.go create mode 100644 go-by-example/functions/functions.go create mode 100644 go-by-example/hello-world/hello-world.go create mode 100644 go-by-example/maps/maps.go create mode 100644 go-by-example/slices/slices.go create mode 100644 go-by-example/switch/switch.go create mode 100644 go-by-example/values/values.go create mode 100644 go-by-example/variables/variables.go diff --git a/go-by-example/arrays/arrays.go b/go-by-example/arrays/arrays.go new file mode 100644 index 0000000..89d0421 --- /dev/null +++ b/go-by-example/arrays/arrays.go @@ -0,0 +1,38 @@ +package main + +import "fmt" + +func main() { + + var a [5]int + fmt.Println("emp:", a) + + a[4] = 100 + fmt.Println("set:", a) + fmt.Println("get:", a[4]) + + fmt.Println("len:", len(a)) + + b := [5]int{1, 2, 3, 4, 5} + fmt.Println("dcl:", b) + // Compiler counts for you. + b = [...]int{1, 2, 3, 4, 5} + fmt.Println("dcl:", b) + + b = [...]int{100, 3: 400, 500} + fmt.Println("idx:", b) + + var twoD [2][3]int + for i := range 2 { + for j := range 3 { + twoD[i][j] = i + j + } + } + fmt.Println("2d: ", twoD) + + twoD = [2][3]int{ + {1, 2, 3}, + {1, 2, 3}, + } + fmt.Println("2d: ", twoD) +} diff --git a/go-by-example/constants/constants.go b/go-by-example/constants/constants.go new file mode 100644 index 0000000..661e8c5 --- /dev/null +++ b/go-by-example/constants/constants.go @@ -0,0 +1,22 @@ +package main + +import ( + "fmt" + "math" +) + +const s string = "constant" + +func main() { + fmt.Println(s) + + const n = 500000000 + + const d = 3e20 / n + + fmt.Println(d) + + fmt.Println(int64(d)) + + fmt.Println(math.Sin(n)) +} diff --git a/go-by-example/for/for.go b/go-by-example/for/for.go new file mode 100644 index 0000000..14559b2 --- /dev/null +++ b/go-by-example/for/for.go @@ -0,0 +1,35 @@ +package main + +import "fmt" + +func main() { + + // Basic + i := 1 + for i <= 3 { + fmt.Println(i) + i = i + 1 + } + + // Classic initial/conditional/after loop + for j := 0; j < 3; j++ { + fmt.Println(j) + } + // or modernized + for j := range 3 { + fmt.Println("range", j) + } + // Will run until break + for { + fmt.Println("Break") + break + } + // Continue + for n := range 10 { + if n%2 == 0 { + continue + } + fmt.Println(n) + } + +} diff --git a/go-by-example/functions/functions.go b/go-by-example/functions/functions.go new file mode 100644 index 0000000..ef4e6f1 --- /dev/null +++ b/go-by-example/functions/functions.go @@ -0,0 +1,33 @@ +package main + +import "fmt" + +func plus(a int, b int) int { + return a + b +} + +func plusPlus(a, b, c int) int { + return a + b + c +} + +func vals() (int, int) { + return 3, 7 +} + +func main() { + + res := plus(1, 2) + fmt.Println("1 + 2 =", res) + + res = plusPlus(1, 2, 3) + fmt.Println("1 + 2 + 3 =", res) + + a, b := vals() + + fmt.Println(a) + fmt.Println(b) + + _, c := vals() + fmt.Println(c) + +} diff --git a/go-by-example/hello-world/hello-world.go b/go-by-example/hello-world/hello-world.go new file mode 100644 index 0000000..a3dd973 --- /dev/null +++ b/go-by-example/hello-world/hello-world.go @@ -0,0 +1,7 @@ +package main + +import "fmt" + +func main() { + fmt.Println("Hello, World!") +} diff --git a/go-by-example/maps/maps.go b/go-by-example/maps/maps.go new file mode 100644 index 0000000..03ee8c2 --- /dev/null +++ b/go-by-example/maps/maps.go @@ -0,0 +1,42 @@ +package main + +import ( + "fmt" + "maps" +) + +func main() { + + m := make(map[string]int) + + m["k1"] = 7 + m["k2"] = 13 + + fmt.Println("map:", m) + + v1 := m["k1"] + fmt.Println("v1", v1) + + v3 := m["k3"] + fmt.Println("v3:", v3) + + fmt.Println("len:", len(m)) + + delete(m, "k2") + fmt.Println("map:", m) + + clear(m) + fmt.Println("map:", m) + + _, prs := m["k2"] + fmt.Println("prs:", prs) + + n := map[string]int{"foo": 1, "bar": 2} + fmt.Println("map:", n) + + n2 := map[string]int{"foo": 1, "bar": 2} + + if maps.Equal(n, n2) { + fmt.Println("n == n2") + } +} diff --git a/go-by-example/slices/slices.go b/go-by-example/slices/slices.go new file mode 100644 index 0000000..8c084dd --- /dev/null +++ b/go-by-example/slices/slices.go @@ -0,0 +1,48 @@ +package main + +import ( + "fmt" + // "slices" +) + +func main() { + + var s []string + fmt.Println("uninit", s, s == nil, len(s) == 0) + + s = make([]string, 3) + fmt.Println("emp:", s, "len:", len(s), "cap:", cap(s)) + + s[0] = "a" + s[1] = "b" + s[2] = "c" + + fmt.Println("set:", s) + fmt.Println("get:", s[2]) + + s = append(s, "d") + s = append(s, "e", "f") + + fmt.Println("apd:", s) + + f := make([]int, 3, 10) + fmt.Println("f:", f, "len:", len(f), "cap:", cap(f)) + + c := make([]string, len(s)) + copy(c, s) + fmt.Println("cpy:", c) + + l := s[2:5] + fmt.Println("sl1:", l) + // two d slices + twoD := make([][]int, 10) + + for i := range len(twoD) { + innerLen := i + 1 + twoD[i] = make([]int, innerLen) + for j := range innerLen { + twoD[i][j] = 1 + j + } + } + fmt.Println("2d: ", twoD) +} diff --git a/go-by-example/switch/switch.go b/go-by-example/switch/switch.go new file mode 100644 index 0000000..545f1a6 --- /dev/null +++ b/go-by-example/switch/switch.go @@ -0,0 +1,49 @@ +package main + +import ( + "fmt" + "time" +) + +func main() { + + i := 2 + fmt.Print("Write ", i, " as ") + switch i { + case 1: + fmt.Println("one") + case 2: + fmt.Println("two") + case 3: + fmt.Println("three") + } + + switch time.Now().Weekday() { + case time.Saturday, time.Sunday: + fmt.Println("It's the weekend") + default: + fmt.Println("It's a weekday") + } + + t := time.Now() + switch { + case t.Hour() < 12: + fmt.Println("It's before noon") + default: + fmt.Println("It's after noon") + } + + whatAmI := func(i interface{}) { + switch t := i.(type) { + case bool: + fmt.Println("I'm a bool") + case int: + fmt.Println("I'm an int") + default: + fmt.Printf("Don't know type %T\n", t) + } + } + whatAmI(true) + whatAmI(1) + whatAmI("hey") +} diff --git a/go-by-example/values/values.go b/go-by-example/values/values.go new file mode 100644 index 0000000..b9c0634 --- /dev/null +++ b/go-by-example/values/values.go @@ -0,0 +1,17 @@ +package main + +import "fmt" + +func main() { + + fmt.Println("go" + "lang") + + fmt.Println("1+1 =", 1+1) + + fmt.Println("7.0/3.0 =", 7.0/3.0) + + fmt.Println(true && false) + fmt.Println(true || false) + fmt.Println(!true) + +} diff --git a/go-by-example/variables/variables.go b/go-by-example/variables/variables.go new file mode 100644 index 0000000..bb4dabe --- /dev/null +++ b/go-by-example/variables/variables.go @@ -0,0 +1,22 @@ +package main + +import "fmt" + +func main() { + + var a = "initial" + fmt.Println(a) + + var b, c int = 1, 2 + fmt.Println(b, c) + + var d = true + fmt.Println(d) + + var e int + fmt.Println(e) + + f := "apple" + fmt.Println(f) + +}