forked from steger/pr3-ws202627
Compare commits
13 Commits
3492da5e5d
...
78cbe47176
| Author | SHA1 | Date |
|---|---|---|
|
|
78cbe47176 | |
|
|
bf881b9671 | |
|
|
623b10273f | |
|
|
e17734dab4 | |
|
|
96daf2f7e2 | |
|
|
7d84b5be29 | |
|
|
81a12a4f06 | |
|
|
eaa0a8b7de | |
|
|
e3a517a931 | |
|
|
efb04df47d | |
|
|
e220ea6031 | |
|
|
f335aace32 | |
|
|
34a8506c81 |
|
|
@ -0,0 +1,9 @@
|
||||||
|
{
|
||||||
|
"features": {
|
||||||
|
"ghcr.io/devcontainers/features/docker-in-docker:2": {
|
||||||
|
"version": "2.17.0",
|
||||||
|
"resolved": "ghcr.io/devcontainers/features/docker-in-docker@sha256:25b9f05705ffba7dbe503230ac76081419306f8c8bc88e0ce78c4ecd99a0c78c",
|
||||||
|
"integrity": "sha256:25b9f05705ffba7dbe503230ac76081419306f8c8bc88e0ce78c4ecd99a0c78c"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,26 @@
|
||||||
|
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
|
||||||
|
// README at: https://github.com/devcontainers/templates/tree/main/src/docker-in-docker
|
||||||
|
{
|
||||||
|
"name": "Docker in Docker",
|
||||||
|
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
|
||||||
|
"image": "mcr.microsoft.com/devcontainers/base:bookworm",
|
||||||
|
|
||||||
|
"features": {
|
||||||
|
"ghcr.io/devcontainers/features/docker-in-docker:2": {
|
||||||
|
"version": "latest",
|
||||||
|
"moby": "true"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use 'forwardPorts' to make a list of ports inside the container available locally.
|
||||||
|
// "forwardPorts": [],
|
||||||
|
|
||||||
|
// Use 'postCreateCommand' to run commands after the container is created.
|
||||||
|
// "postCreateCommand": "docker --version",
|
||||||
|
|
||||||
|
// Configure tool-specific properties.
|
||||||
|
// "customizations": {},
|
||||||
|
|
||||||
|
// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
|
||||||
|
// "remoteUser": "root"
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,12 @@
|
||||||
|
# To get started with Dependabot version updates, you'll need to specify which
|
||||||
|
# package ecosystems to update and where the package manifests are located.
|
||||||
|
# Please see the documentation for more information:
|
||||||
|
# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates
|
||||||
|
# https://containers.dev/guide/dependabot
|
||||||
|
|
||||||
|
version: 2
|
||||||
|
updates:
|
||||||
|
- package-ecosystem: "devcontainers"
|
||||||
|
directory: "/"
|
||||||
|
schedule:
|
||||||
|
interval: weekly
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
FROM golang:1.27-bookworm
|
FROM golang:1.27-bookworm
|
||||||
|
|
||||||
RUN apt-get update \
|
RUN apt-get update \
|
||||||
&& apt-get install -y build-essential \
|
&& apt-get install -y build-essential ripgrep \
|
||||||
&& rm -rf /var/lib/apt/lists/*
|
&& rm -rf /var/lib/apt/lists/*
|
||||||
|
|
||||||
RUN apt-get update \
|
RUN apt-get update \
|
||||||
|
|
|
||||||
|
|
@ -5,11 +5,16 @@
|
||||||
},
|
},
|
||||||
"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": { "extensions": [
|
"vscode": {
|
||||||
"golang.go",
|
"extensions": [
|
||||||
"gruntfuggly.todo-tree",
|
"golang.go",
|
||||||
"-github.copilot"
|
"gruntfuggly.todo-tree",
|
||||||
]}
|
"-github.copilot"
|
||||||
|
],
|
||||||
|
"settings": {
|
||||||
|
"todo-tree.ripgrep.ripgrep": "/usr/bin/rg"
|
||||||
|
}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"remoteUser": "developer",
|
"remoteUser": "developer",
|
||||||
"updateRemoteUserUID": true
|
"updateRemoteUserUID": true
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,11 @@ package main
|
||||||
|
|
||||||
import "fmt"
|
import "fmt"
|
||||||
|
|
||||||
func main() {
|
//go:fix inline
|
||||||
fmt.Println("hello world")
|
func varOf(x int) *int {
|
||||||
|
return new(x)
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fmt.Println(new(10))
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,11 @@ func plus(a int, b int) int {
|
||||||
// return a + b
|
// return a + b
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
func plus2(a int, b int) (result int) {
|
||||||
|
result = a + b
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// A function with multiple parameters of the same type can be shortened by listing the type only once.
|
// A function with multiple parameters of the same type can be shortened by listing the type only once.
|
||||||
func plusPlus(a, b, c int) int {
|
func plusPlus(a, b, c int) int {
|
||||||
return a + b + c
|
return a + b + c
|
||||||
|
|
@ -26,6 +31,10 @@ func plusNamed(a, b int) (result int) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func nonplusultra(a int, b int) (result int, description string) {
|
||||||
|
return a + b, fmt.Sprintf("the sum of %d and %d is %d, a, b, a+b")
|
||||||
|
}
|
||||||
|
|
||||||
// A function can return multiple values. Here we return the sum and a formatted string description of the operation.
|
// A function can return multiple values. Here we return the sum and a formatted string description of the operation.
|
||||||
func plusDescription(a int, b int) (int, string) {
|
func plusDescription(a int, b int) (int, string) {
|
||||||
result := a + b
|
result := a + b
|
||||||
|
|
@ -36,6 +45,7 @@ func main() {
|
||||||
|
|
||||||
res := plus(1, 2)
|
res := plus(1, 2)
|
||||||
fmt.Println("1+2 =", res)
|
fmt.Println("1+2 =", res)
|
||||||
|
fmt.Println(plus(30, 18))
|
||||||
|
|
||||||
res = plusPlus(1, 2, 3)
|
res = plusPlus(1, 2, 3)
|
||||||
fmt.Println("1+2+3 =", res)
|
fmt.Println("1+2+3 =", res)
|
||||||
|
|
|
||||||
|
|
@ -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()
|
||||||
|
}
|
||||||
|
|
@ -0,0 +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
|
||||||
|
|
||||||
|
//the following code just demonstrates how to use fmt.Scan
|
||||||
|
|
||||||
|
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")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,3 @@
|
||||||
|
module gitty.informatik.th-mannheim.de/steger/pr3-sose2026/go
|
||||||
|
|
||||||
|
go 1.26.0
|
||||||
|
|
@ -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")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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)
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,23 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
type Counter struct {
|
||||||
|
value int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c Counter /** value reciever **/) toString() string {
|
||||||
|
return fmt.Sprintf("Counter Value: %d", c.value)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Counter /**Pointer reciever**/) Increment() {
|
||||||
|
fmt.Printf("Inside Increment: %p\n", c)
|
||||||
|
c.value = c.value + 1
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
c := Counter{}
|
||||||
|
fmt.Println(c)
|
||||||
|
c.Increment()
|
||||||
|
fmt.Println(c)
|
||||||
|
}
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
module gitty.informatik.th-mannheim.de/steger/pr3-ws202627/go/03-modules
|
module gitty.informatik.th-mannheim.de/3010261/pr3-ws202627/go/03-modules
|
||||||
|
|
||||||
go 1.25.0
|
go 1.25.0
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@ package myMath_test
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"gitty.informatik.hs-mannheim.de/steger/pr3-code/go/03-modules/myMath"
|
"gitty.informatik.hs-mannheim.de/3010261/pr3-code/go/03-modules/myMath"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestAdd(t *testing.T) {
|
func TestAdd(t *testing.T) {
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,3 @@
|
||||||
|
module gitty.informatik.th-mannheim.de/3010261/pr3-ws202627/go/04-design-pattern
|
||||||
|
|
||||||
|
go 1.27.3
|
||||||
|
|
@ -0,0 +1,29 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"gitty.informatik.th-mannheim.de/3010261/pr3-ws202627/go/04-design-pattern/patterns"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
noodle, err := patterns.NewNoodleMaker().
|
||||||
|
Name("Spaghetti").
|
||||||
|
Type("Lang").
|
||||||
|
Length(25).
|
||||||
|
Girth(2).
|
||||||
|
Build()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Fehler:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fmt.Printf("%+v\n", noodle)
|
||||||
|
|
||||||
|
standard, _ := patterns.NewNoodleMaker().Build()
|
||||||
|
fmt.Printf("%+v\n", standard)
|
||||||
|
|
||||||
|
_, err = patterns.NewNoodleMaker().Length(0).Build()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Erwarteter Fehler:", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,48 @@
|
||||||
|
package patterns
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Noodle struct {
|
||||||
|
Type string
|
||||||
|
Name string
|
||||||
|
Length int
|
||||||
|
Girth int
|
||||||
|
}
|
||||||
|
|
||||||
|
type NoodleMaker struct {
|
||||||
|
noodle Noodle
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewNoodleMaker() *NoodleMaker {
|
||||||
|
return &NoodleMaker{
|
||||||
|
noodle: Noodle{"Röhre", "Fusilli", 10, 5},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *NoodleMaker) Length(l int) *NoodleMaker {
|
||||||
|
b.noodle.Length = l
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
func (c *NoodleMaker) Girth(g int) *NoodleMaker {
|
||||||
|
c.noodle.Girth = g
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
func (d *NoodleMaker) Name(n string) *NoodleMaker {
|
||||||
|
d.noodle.Name = n
|
||||||
|
return d
|
||||||
|
}
|
||||||
|
func (e *NoodleMaker) Type(t string) *NoodleMaker {
|
||||||
|
e.noodle.Type = t
|
||||||
|
return e
|
||||||
|
}
|
||||||
|
func (m *NoodleMaker) Build() (Noodle, error) {
|
||||||
|
if m.noodle.Length <= 0 {
|
||||||
|
return Noodle{}, errors.New("Länge muss größer als 0 sein")
|
||||||
|
}
|
||||||
|
if m.noodle.Girth <= 0 {
|
||||||
|
return Noodle{}, errors.New("Dicke muss größer als 0 sein")
|
||||||
|
}
|
||||||
|
return m.noodle, nil
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,48 @@
|
||||||
|
package patterns
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestNoodleMaker(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
build func() (Noodle, error)
|
||||||
|
want Noodle
|
||||||
|
wantErr bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "Defaults",
|
||||||
|
build: func() (Noodle, error) { return newNoodleMaker().Build() },
|
||||||
|
want: Noodle{Type: "Röhre", Name: "Fusilli", Length: 10, Girth: 5},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "New Noodle",
|
||||||
|
build: func() (Noodle, error) {
|
||||||
|
return newNoodleMaker().Name("Spaghetti").Type("Lang").Length(25).Girth(2).Build()
|
||||||
|
},
|
||||||
|
want: Noodle{Type: "Lang", Name: "Spaghetti", Length: 25, Girth: 2},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Length 0",
|
||||||
|
build: func() (Noodle, error) { return newNoodleMaker().Length(0).Build() },
|
||||||
|
wantErr: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Girth -1",
|
||||||
|
build: func() (Noodle, error) { return newNoodleMaker().Girth(-1).Build() },
|
||||||
|
wantErr: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
got, err := tt.build()
|
||||||
|
if (err != nil) != tt.wantErr {
|
||||||
|
t.Fatalf("error = %v, wantErr %v", err, tt.wantErr)
|
||||||
|
}
|
||||||
|
if !tt.wantErr && got != tt.want {
|
||||||
|
t.Errorf("got %+v, want %+v", got, tt.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,17 +1,27 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
func print(ci <-chan int) {
|
func print(ci <-chan int) {
|
||||||
//TODO: implement
|
//TODO: implement
|
||||||
|
for value := range ci {
|
||||||
|
time.Sleep(100 * time.Millisecond)
|
||||||
|
fmt.Println(value)
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
c := make(chan int)
|
c := make(chan int)
|
||||||
|
done := make(chan bool)
|
||||||
|
go func() { print(c); done <- true; close(done) }()
|
||||||
c <- 1
|
c <- 1
|
||||||
c <- 2
|
c <- 2
|
||||||
c <- 3
|
c <- 3
|
||||||
close(c)
|
close(c)
|
||||||
|
<-done
|
||||||
print(c)
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"sort"
|
"sort"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -22,6 +23,54 @@ func mergeSortSequential(data []float64) {
|
||||||
copy(data, merge(left, right))
|
copy(data, merge(left, right))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func mergeSortParallel(data []float64) {
|
||||||
|
if len(data) <= 1000 {
|
||||||
|
mergeSortSequential(data)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
mid := len(data) / 2
|
||||||
|
left := data[:mid]
|
||||||
|
right := data[mid:]
|
||||||
|
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
wg.Add(2)
|
||||||
|
|
||||||
|
go func() { mergeSortParallel(left); wg.Done() }()
|
||||||
|
|
||||||
|
go func() { mergeSortParallel(right); wg.Done() }()
|
||||||
|
|
||||||
|
wg.Wait()
|
||||||
|
|
||||||
|
copy(data, merge(left, right))
|
||||||
|
}
|
||||||
|
|
||||||
|
func mergeSortChannelsInternal(data []float64, sorted chan<- float64) {
|
||||||
|
|
||||||
|
if len(data) < 1000 {
|
||||||
|
sortedData := make([]float64, len(data))
|
||||||
|
copy(sortedData, data)
|
||||||
|
mergeSortSequential(sortedData)
|
||||||
|
|
||||||
|
for _, := 0; i < count; i++ {
|
||||||
|
|
||||||
|
}
|
||||||
|
close(sorted)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func mergeSortChannels(data []float64) {
|
||||||
|
sorted := make(chan float64)
|
||||||
|
go mergeSortChannelsInternal(data, sorted)
|
||||||
|
sortedData := make([]float64, len(data))
|
||||||
|
i := 0
|
||||||
|
for x := range sorted {
|
||||||
|
sortedData[i] = x
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
copy(data, sortedData)
|
||||||
|
}
|
||||||
|
|
||||||
func merge(left, right []float64) []float64 {
|
func merge(left, right []float64) []float64 {
|
||||||
result := make([]float64, 0, len(left)+len(right))
|
result := make([]float64, 0, len(left)+len(right))
|
||||||
i, j := 0, 0
|
i, j := 0, 0
|
||||||
|
|
@ -58,7 +107,8 @@ func main() {
|
||||||
//2. SORTING
|
//2. SORTING
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
|
|
||||||
mergeSortSequential(data)
|
//mergeSortSequential(data)
|
||||||
|
mergeSortParallel(data)
|
||||||
|
|
||||||
elapsed := time.Since(start)
|
elapsed := time.Since(start)
|
||||||
fmt.Printf("MergeSort took %s\n", elapsed)
|
fmt.Printf("MergeSort took %s\n", elapsed)
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
@ -8,12 +9,33 @@ import (
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
ch1 := make(chan string)
|
ch1 := make(chan string)
|
||||||
|
ch2 := make(chan string)
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
time.Sleep(time.Duration(rand.Intn(5)+1) * time.Second)
|
time.Sleep(time.Duration(rand.Intn(5)+1) * time.Second)
|
||||||
ch1 <- "Message from channel 1"
|
ch1 <- "Message from channel 1"
|
||||||
}()
|
}()
|
||||||
|
|
||||||
msg1 := <-ch1
|
go func() {
|
||||||
println("Received:", msg1)
|
time.Sleep(time.Duration(rand.Intn(5)+1) * time.Second)
|
||||||
|
ch2 <- "Message from channel 2"
|
||||||
|
}()
|
||||||
|
|
||||||
|
count := 10
|
||||||
|
var channels []chan string
|
||||||
|
for i := range count {
|
||||||
|
ch := make(chan string)
|
||||||
|
channels = append(channels, ch)
|
||||||
|
go func() {
|
||||||
|
time.Sleep(time.Duration(rand.Intn(5)+1) * time.Second)
|
||||||
|
ch <- fmt.Sprintln("Message from Channel %d", i)
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
|
fanIn := make(chan string, count)
|
||||||
|
for i := range count {
|
||||||
|
go func() { fanIn <- <-channels[i] }()
|
||||||
|
}
|
||||||
|
println("Recieved: ", <-fanIn)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue