From f335aace32aaeba179ab26fdb405d28ef41f75e5 Mon Sep 17 00:00:00 2001 From: ChaosLL1337 Date: Wed, 7 Oct 2026 08:27:43 +0200 Subject: [PATCH] Aufgaben bearbeitet --- go/00-hello-world/hello-world.go | 9 ++- go/01-basics/basics.go | 118 +++++++++++++++++++++++++++++++ go/01-basics/calculator.go | 78 ++++++++++++++++---- 3 files changed, 190 insertions(+), 15 deletions(-) create mode 100644 go/01-basics/basics.go diff --git a/go/00-hello-world/hello-world.go b/go/00-hello-world/hello-world.go index c048119..df5bff4 100644 --- a/go/00-hello-world/hello-world.go +++ b/go/00-hello-world/hello-world.go @@ -2,6 +2,11 @@ package main import "fmt" -func main() { - fmt.Println("hello world") +//go:fix inline +func varOf(x int) *int { + return new(x) +} + +func main() { + fmt.Println(new(10)) } diff --git a/go/01-basics/basics.go b/go/01-basics/basics.go new file mode 100644 index 0000000..91c2318 --- /dev/null +++ b/go/01-basics/basics.go @@ -0,0 +1,118 @@ +package main + +import ( + "fmt" + "math" +) + +const h string = "constant" + +// zeroval bekommt eine KOPIE: Änderung wirkt nur lokal. +func zeroval(ival int) { + ival = 0 +} + +// zeroptr bekommt die ADRESSE: Änderung wirkt auf das Original. +func zeroptr(iptr *int) { + *iptr = 0 +} + +func basics() { + fmt.Println("go", "lang") + fmt.Println("go" + "lang") + fmt.Println(7 / 3) // 2 (Ganzzahldivision) + fmt.Println(true || false) + fmt.Println(!true) +} + +func variables() { + var a = "Initial" // Typ wird inferiert + fmt.Println(a) + a = "string" + fmt.Println(a) + + var b int // Nullwert: 0 + var c = 4 + var x, y, z int = 1, 3, 6 + fmt.Println(b, c) + fmt.Println(x, y, z) + + s, z := "string", 8 // s neu, z nur neu zugewiesen + fmt.Println(s, z) + + svar := h + fmt.Println(svar) +} + +func constants() { + const n = 500000 + const q = 3e20 / n // untypisiert, wird zur Compile-Zeit berechnet + fmt.Println(q) // 6e+14 + i := int64(n) + fmt.Println(math.Sin(float64(i))) + fmt.Println(math.Sin(n)) // untypisierte Konstante passt sich an +} + +func controlFlow() { + // Schleife mit Abbruchbedingung + count := 0 + for { + fmt.Println("hello") + count++ + if count >= 3 { + break + } + } + + j := 2 + switch j { + case 1: + fmt.Println("one") + case 2, 3: + fmt.Println("two or three") + default: + fmt.Println("other") + } +} + +func arrays() { + var u [5]int + u[2] = 4 + v := [...]int{1, 3, 4, 5, 6, 3} // Länge wird vom Compiler gezählt + fmt.Println(u, v, len(v)) +} + +func pointers() { + t := 1 + zeroval(t) + fmt.Println(t) // 1, unverändert + zeroptr(&t) + fmt.Println(t) // 0, verändert + + j := 2 + p := &j + fmt.Println(*p) // 2 (Dereferenzierung) +} +func slices() { + a := []int{} + a = append(a, 5, 6, 6, 9, 7, 8) + a = append(a, 6) + a = append(a, 7) + a[3] = 99 + a = append(a, 8) + b := a + a = append(a, 9) + fmt.Println(a, len(a)) + fmt.Println(b) + +} + +func main() { + basics() + variables() + constants() + controlFlow() + arrays() + pointers() + slices() +} diff --git a/go/01-basics/calculator.go b/go/01-basics/calculator.go index 7686df8..f9bb191 100644 --- a/go/01-basics/calculator.go +++ b/go/01-basics/calculator.go @@ -1,22 +1,74 @@ package main - + import ( "fmt" ) - + +const ( + opAdd = "add" + opSubtract = "subtract" + opMultiply = "multiply" + opDivide = "divide" + opExit = "exit" +) + +var result float64 + +func add(a, b float64) { + result = a + b +} +func multiply(a, b float64) { + result = a * b +} +func subtract(a, b float64) { + result = a - b +} +func divide(a, b float64) { + result = a / b +} + func main() { + var x float64 + var y float64 - //TODO: implement according to README.md + //TODO: implement according to README.md //the following code just demonstrates how to use fmt.Scan - var str string - fmt.Print("Please enter a string: ") - fmt.Scan(&str) - - var x float64 - fmt.Print("Please enter a float: ") - fmt.Scan(&x) - - fmt.Printf("You entered '%s' and %f\n", str, x) -} + for { + fmt.Print("What would you like to do? (add|subtract|divide|multiply|exit)") + var str string + fmt.Scan(&str) + if str == opExit { + break + } else { + + fmt.Print("Please enter a float: ") + fmt.Scan(&x) + + fmt.Print("Please enter a float: ") + fmt.Scan(&y) + + switch str { + case opAdd: + add(x, y) + fmt.Println(result) + case opMultiply: + multiply(x, y) + fmt.Println(result) + case opDivide: + if y == 0 { + fmt.Println("Division by 0 is not possible") + continue + } + divide(x, y) + fmt.Println(result) + case opSubtract: + subtract(x, y) + fmt.Println(result) + default: + fmt.Println("Unknown operation") + } + } + } +} \ No newline at end of file