From a15c5c65383f14c502930438c19e89bf3efa8499 Mon Sep 17 00:00:00 2001 From: Sebastian Steger Date: Wed, 20 Aug 2025 09:20:37 +0000 Subject: [PATCH 1/4] implement myMath module --- go/03-modules/go.mod | 5 +++++ go/03-modules/go.sum | 2 ++ go/03-modules/main.go | 16 +++++++++++++- go/03-modules/myMath/math_test.go | 35 +++++++++++++++++++++++++++++++ 4 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 go/03-modules/go.mod create mode 100644 go/03-modules/go.sum create mode 100644 go/03-modules/myMath/math_test.go diff --git a/go/03-modules/go.mod b/go/03-modules/go.mod new file mode 100644 index 0000000..b93913c --- /dev/null +++ b/go/03-modules/go.mod @@ -0,0 +1,5 @@ +module gitty.informatik.hs-mannheim.de/steger/pr3-code/go/03-modules/myMath + +go 1.25.0 + +require github.com/google/uuid v1.6.0 diff --git a/go/03-modules/go.sum b/go/03-modules/go.sum new file mode 100644 index 0000000..7790d7c --- /dev/null +++ b/go/03-modules/go.sum @@ -0,0 +1,2 @@ +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= diff --git a/go/03-modules/main.go b/go/03-modules/main.go index 7905807..ab01282 100644 --- a/go/03-modules/main.go +++ b/go/03-modules/main.go @@ -1,5 +1,19 @@ package main -func main() { +import ( + "fmt" + mm "gitty.informatik.hs-mannheim.de/steger/pr3-code/go/03-modules/myMath" + //_ "gitty.informatik.hs-mannheim.de/steger/pr3-code/go/03-modules/myMath" + + "github.com/google/uuid" + //run go get github.com/google/uuid + //or go mod tidy => go.sum +) + +func main() { + fmt.Println(mm.Add(1, 2)) + + id := uuid.New() + fmt.Println("Generated UUID:", id) } diff --git a/go/03-modules/myMath/math_test.go b/go/03-modules/myMath/math_test.go new file mode 100644 index 0000000..cf3fe11 --- /dev/null +++ b/go/03-modules/myMath/math_test.go @@ -0,0 +1,35 @@ +package myMath_test + +import ( + "testing" + + "gitty.informatik.hs-mannheim.de/steger/pr3-code/go/03-modules/myMath" +) + +func TestAdd(t *testing.T) { + sum := myMath.Add(2, 3) + if sum != 5 { + t.Errorf("Add(2,3) = %v, want %v", sum, 5) + } +} + +func TestMin(t *testing.T) { + type args struct { + in0 int + in1 int + } + tests := []struct { + name string + args args + want int + }{ + {"first greater", args{3, 2}, 2}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := myMath.Min(tt.args.in0, tt.args.in1); got != tt.want { + t.Errorf("Min() = %v, want %v", got, tt.want) + } + }) + } +} From 0f90d8f25bb6ceeec8a9d4a434aecfa87b2dc428 Mon Sep 17 00:00:00 2001 From: Sebastian Steger Date: Wed, 20 Aug 2025 09:20:59 +0000 Subject: [PATCH 2/4] implement myMath module II --- go/03-modules/myMath/math.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/go/03-modules/myMath/math.go b/go/03-modules/myMath/math.go index e69de29..de9b318 100644 --- a/go/03-modules/myMath/math.go +++ b/go/03-modules/myMath/math.go @@ -0,0 +1,23 @@ +package myMath + +import "fmt" + +func init() { + fmt.Println("initializing myMath") +} + +func Add(a, b int) int { + return a + b +} + +func greater(a, b int) bool { + return a > b +} + +func Min(a, b int) int { + if greater(a, b) { + return b + } else { + return a + } +} From 50100a303571379c5d84f71295c1dac3b3ef22e2 Mon Sep 17 00:00:00 2001 From: Sebastian Steger Date: Wed, 20 Aug 2025 11:13:53 +0000 Subject: [PATCH 3/4] fix go.mod --- go/03-modules/go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go/03-modules/go.mod b/go/03-modules/go.mod index b93913c..e56e608 100644 --- a/go/03-modules/go.mod +++ b/go/03-modules/go.mod @@ -1,4 +1,4 @@ -module gitty.informatik.hs-mannheim.de/steger/pr3-code/go/03-modules/myMath +module gitty.informatik.hs-mannheim.de/steger/pr3-code/go/03-modules go 1.25.0 From eeb9217f9fa6a0cb3c679a071cfef133cbdc8576 Mon Sep 17 00:00:00 2001 From: Sebastian Steger Date: Tue, 7 Apr 2026 13:31:16 +0000 Subject: [PATCH 4/4] comments --- go/03-modules/main.go | 10 +++++++++- go/03-modules/myMath/math.go | 4 ++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/go/03-modules/main.go b/go/03-modules/main.go index ab01282..923a1d4 100644 --- a/go/03-modules/main.go +++ b/go/03-modules/main.go @@ -3,9 +3,17 @@ package main import ( "fmt" + // importing the myMath package with an alias mm. This allows us to use mm instead of myMath to access the functions in the package. + // go packages are imported using their full path, which is the path to the package relative to the GOPATH or module root. . mm "gitty.informatik.hs-mannheim.de/steger/pr3-code/go/03-modules/myMath" - //_ "gitty.informatik.hs-mannheim.de/steger/pr3-code/go/03-modules/myMath" + // the underscore is used to import a package solely for its side effects (i.e., the init function). + // This is useful when you want to initialize a package but do not need to access any of its exported functions or variables. + // _ "gitty.informatik.hs-mannheim.de/steger/pr3-code/go/03-modules/myMath" + + // importing a third-party package from GitHub. This package provides functions for generating UUIDs (Universally Unique Identifiers). + // To use this package, you need to run go get github.com/google/uuid to download and install the package in your Go workspace. + // Alternatively, you can run go mod tidy to automatically download and install any missing dependencies based on the imports in your code. "github.com/google/uuid" //run go get github.com/google/uuid //or go mod tidy => go.sum diff --git a/go/03-modules/myMath/math.go b/go/03-modules/myMath/math.go index de9b318..f348cd8 100644 --- a/go/03-modules/myMath/math.go +++ b/go/03-modules/myMath/math.go @@ -2,14 +2,18 @@ package myMath import "fmt" +// init is a special function that is called automatically when the package is imported. It is used to initialize the package +// and can be used to set up any necessary state or perform any necessary setup before the package is used. func init() { fmt.Println("initializing myMath") } +// functions that start with an uppercase letter are exported and can be accessed from outside the package func Add(a, b int) int { return a + b } +// functions that start with a lowercase letter are not exported and cannot be accessed from outside the package func greater(a, b int) bool { return a > b }