From e220ea6031aeab45da9f2c845024847f25866af1 Mon Sep 17 00:00:00 2001 From: ChaosLL1337 Date: Wed, 14 Oct 2026 08:44:49 +0200 Subject: [PATCH] Calcu und Inventory --- go/01-basics/go.mod | 3 ++ go/01-basics/inventory.go | 36 ++++++++++++++++++++---- go/01-basics/maps.go | 41 +++++++++++++++++++++++++++ go/01-basics/slices.go | 58 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 133 insertions(+), 5 deletions(-) create mode 100644 go/01-basics/go.mod create mode 100644 go/01-basics/maps.go create mode 100644 go/01-basics/slices.go diff --git a/go/01-basics/go.mod b/go/01-basics/go.mod new file mode 100644 index 0000000..3120003 --- /dev/null +++ b/go/01-basics/go.mod @@ -0,0 +1,3 @@ +module gitty.informatik.th-mannheim.de/steger/pr3-sose2026/go + +go 1.26.0 diff --git a/go/01-basics/inventory.go b/go/01-basics/inventory.go index 209c374..21ed0b4 100644 --- a/go/01-basics/inventory.go +++ b/go/01-basics/inventory.go @@ -1,40 +1,66 @@ package main +import ( + "fmt" +) + type Product struct { Name string Price float64 Quantity int - Category string //TODO: use enum instead + Category int //TODO: use enum instead } +const( + Electronics int = iota + Groceries + Clothes +) + func addProduct(inventory *[]Product, name string, price float64, quantity int, category string) { //TODO: implement + var l int = inventory.len + inventory = append(inventory, name, price, quantity, category) } func removeProduct(inventory *[]Product, name string) { //TODO: implement + inventory = inventory.delete(name) } func updateQuantity(inventory *[]Product, name string, newQuantity int) { //TODO: implement + sliceInventory[name].quantity = newQuantity; } func displayInventory(inventory []Product) { //TODO: implement + for _, p := range sliceInventory{ + fmt.Println(karte(sliceInventory[p])) + } } func main() { inventory := []Product{ - {Name: "Laptop", Price: 1000, Quantity: 5, Category: "Electronics"}, - {Name: "Apples", Price: 2, Quantity: 50, Category: "Groceries"}, - {Name: "T-shirt", Price: 10, Quantity: 20, Category: "Clothes"}, + {Name: "Laptop", Price: 1000, Quantity: 5, Category: Electronics}, + {Name: "Apples", Price: 2, Quantity: 50, Category: Groceries}, + {Name: "T-shirt", Price: 10, Quantity: 20, Category: Clothes}, + } + var arrInventory = [3]string{"Laptop", "Apples", "T-shirt"} + var sliceInventory = arrInventory[:] + + karte := map[string]Product{ + "Laptop": {Name: "Laptop", Price: 1000, Quantity: 5, Category: Electronics}, + "Apples": {Name: "Apples", Price: 2, Quantity: 50, Category: Groceries}, + "T-shirt": {Name: "T-shirt", Price: 10, Quantity: 20, Category: Clothes}, + } // Display initial inventory displayInventory(inventory) // Add a new product - addProduct(&inventory, "Phone", 800, 10, "Electronics") + addProduct(&inventory, "Phone", 800, 10, Electronics) // Display updated inventory displayInventory(inventory) diff --git a/go/01-basics/maps.go b/go/01-basics/maps.go new file mode 100644 index 0000000..5cff0ad --- /dev/null +++ b/go/01-basics/maps.go @@ -0,0 +1,41 @@ +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") + } +} \ No newline at end of file diff --git a/go/01-basics/slices.go b/go/01-basics/slices.go new file mode 100644 index 0000000..fd0e7cc --- /dev/null +++ b/go/01-basics/slices.go @@ -0,0 +1,58 @@ +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]) + + fmt.Println("len:", len(s)) + + s = append(s, "d") + s = append(s, "e", "f") + fmt.Println("apd:", s) + + c := make([]string, len(s)) + copy(c, s) + fmt.Println("cpy:", c) + + l := s[2:5] + fmt.Println("sl1:", l) + + l = s[:5] + fmt.Println("sl2:", l) + + l = s[2:] + fmt.Println("sl3:", l) + + t := []string{"g", "h", "i"} + fmt.Println("dcl:", t) + + t2 := []string{"g", "h", "i"} + if slices.Equal(t, t2) { + fmt.Println("t == t2") + } + + twoD := make([][]int, 3) + for i := range 3 { + innerLen := i + 1 + twoD[i] = make([]int, innerLen) + for j := range innerLen { + twoD[i][j] = i + j + } + } + fmt.Println("2d: ", twoD) +} \ No newline at end of file