forked from steger/pr3-ws202627
Compare commits
No commits in common. "efb04df47d729b2a32cecf0ae8cd0d08933ff877" and "d123d01a929199f0177aa71009933e46cac9bb09" have entirely different histories.
efb04df47d
...
d123d01a92
|
|
@ -1,7 +1,7 @@
|
||||||
FROM golang:1.26-bookworm
|
FROM golang:1.26-bookworm
|
||||||
|
|
||||||
RUN apt-get update \
|
RUN apt-get update \
|
||||||
&& apt-get install -y build-essential ripgrep \
|
&& apt-get install -y build-essential \
|
||||||
&& rm -rf /var/lib/apt/lists/*
|
&& rm -rf /var/lib/apt/lists/*
|
||||||
|
|
||||||
ARG USERNAME=developer
|
ARG USERNAME=developer
|
||||||
|
|
|
||||||
|
|
@ -5,16 +5,11 @@
|
||||||
},
|
},
|
||||||
"postCreateCommand": "go install github.com/go-delve/delve/cmd/dlv@latest && go install github.com/cweill/gotests/gotests@v1.9.0 && go install golang.org/x/tools/gopls@latest && go install golang.org/x/tools/cmd/goimports@latest && go install honnef.co/go/tools/cmd/staticcheck@latest",
|
"postCreateCommand": "go install github.com/go-delve/delve/cmd/dlv@latest && go install github.com/cweill/gotests/gotests@v1.9.0 && go install golang.org/x/tools/gopls@latest && go install golang.org/x/tools/cmd/goimports@latest && go install honnef.co/go/tools/cmd/staticcheck@latest",
|
||||||
"customizations": {
|
"customizations": {
|
||||||
"vscode": {
|
"vscode": { "extensions": [
|
||||||
"extensions": [
|
"golang.go",
|
||||||
"golang.go",
|
"gruntfuggly.todo-tree",
|
||||||
"gruntfuggly.todo-tree",
|
"-github.copilot"
|
||||||
"-github.copilot"
|
]}
|
||||||
],
|
|
||||||
"settings": {
|
|
||||||
"todo-tree.ripgrep.ripgrep": "/usr/bin/rg"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
"remoteUser": "developer",
|
"remoteUser": "developer",
|
||||||
"updateRemoteUserUID": true
|
"updateRemoteUserUID": true
|
||||||
|
|
|
||||||
|
|
@ -2,11 +2,6 @@ package main
|
||||||
|
|
||||||
import "fmt"
|
import "fmt"
|
||||||
|
|
||||||
//go:fix inline
|
|
||||||
func varOf(x int) *int {
|
|
||||||
return new(x)
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
fmt.Println(new(10))
|
fmt.Println("hello world")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,118 +0,0 @@
|
||||||
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()
|
|
||||||
}
|
|
||||||
|
|
@ -4,71 +4,19 @@ import (
|
||||||
"fmt"
|
"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() {
|
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
|
//the following code just demonstrates how to use fmt.Scan
|
||||||
|
|
||||||
for {
|
var str string
|
||||||
fmt.Print("What would you like to do? (add|subtract|divide|multiply|exit)")
|
fmt.Print("Please enter a string: ")
|
||||||
var str string
|
fmt.Scan(&str)
|
||||||
fmt.Scan(&str)
|
|
||||||
if str == opExit {
|
|
||||||
break
|
|
||||||
} else {
|
|
||||||
|
|
||||||
fmt.Print("Please enter a float: ")
|
var x float64
|
||||||
fmt.Scan(&x)
|
fmt.Print("Please enter a float: ")
|
||||||
|
fmt.Scan(&x)
|
||||||
|
|
||||||
fmt.Print("Please enter a float: ")
|
fmt.Printf("You entered '%s' and %f\n", str, x)
|
||||||
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")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
@ -1,3 +0,0 @@
|
||||||
module gitty.informatik.th-mannheim.de/steger/pr3-sose2026/go
|
|
||||||
|
|
||||||
go 1.26.0
|
|
||||||
|
|
@ -1,66 +1,40 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Product struct {
|
type Product struct {
|
||||||
Name string
|
Name string
|
||||||
Price float64
|
Price float64
|
||||||
Quantity int
|
Quantity int
|
||||||
Category int //TODO: use enum instead
|
Category string //TODO: use enum instead
|
||||||
}
|
}
|
||||||
|
|
||||||
const(
|
|
||||||
Electronics int = iota
|
|
||||||
Groceries
|
|
||||||
Clothes
|
|
||||||
)
|
|
||||||
|
|
||||||
func addProduct(inventory *[]Product, name string, price float64, quantity int, category string) {
|
func addProduct(inventory *[]Product, name string, price float64, quantity int, category string) {
|
||||||
//TODO: implement
|
//TODO: implement
|
||||||
var l int = inventory.len
|
|
||||||
inventory = append(inventory, name, price, quantity, category)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func removeProduct(inventory *[]Product, name string) {
|
func removeProduct(inventory *[]Product, name string) {
|
||||||
//TODO: implement
|
//TODO: implement
|
||||||
inventory = inventory.delete(name)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func updateQuantity(inventory *[]Product, name string, newQuantity int) {
|
func updateQuantity(inventory *[]Product, name string, newQuantity int) {
|
||||||
//TODO: implement
|
//TODO: implement
|
||||||
sliceInventory[name].quantity = newQuantity;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func displayInventory(inventory []Product) {
|
func displayInventory(inventory []Product) {
|
||||||
//TODO: implement
|
//TODO: implement
|
||||||
for _, p := range sliceInventory{
|
|
||||||
fmt.Println(karte(sliceInventory[p]))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
inventory := []Product{
|
inventory := []Product{
|
||||||
{Name: "Laptop", Price: 1000, Quantity: 5, Category: Electronics},
|
{Name: "Laptop", Price: 1000, Quantity: 5, Category: "Electronics"},
|
||||||
{Name: "Apples", Price: 2, Quantity: 50, Category: Groceries},
|
{Name: "Apples", Price: 2, Quantity: 50, Category: "Groceries"},
|
||||||
{Name: "T-shirt", Price: 10, Quantity: 20, Category: Clothes},
|
{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
|
// Display initial inventory
|
||||||
displayInventory(inventory)
|
displayInventory(inventory)
|
||||||
|
|
||||||
// Add a new product
|
// Add a new product
|
||||||
addProduct(&inventory, "Phone", 800, 10, Electronics)
|
addProduct(&inventory, "Phone", 800, 10, "Electronics")
|
||||||
|
|
||||||
// Display updated inventory
|
// Display updated inventory
|
||||||
displayInventory(inventory)
|
displayInventory(inventory)
|
||||||
|
|
|
||||||
|
|
@ -1,41 +0,0 @@
|
||||||
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")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,58 +0,0 @@
|
||||||
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)
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue