summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Schneider <qsx@chaotikum.eu>2025-12-04 17:26:50 +0100
committerThomas Schneider <qsx@chaotikum.eu>2025-12-04 17:26:50 +0100
commitf65158124dad22f8863768fb38260573ab37fa2d (patch)
tree126d3d9aeaaf385e430464d9a62d0407e577027b
Initial importHEADmain
-rw-r--r--.gitignore33
-rw-r--r--go.mod5
-rw-r--r--go.sum2
-rw-r--r--main.go11
-rw-r--r--main_test.go33
5 files changed, 84 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..ade6005
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,33 @@
+### https://github.com/github/gitignore/blob/86922aee426c32ffb6098f9a38c42da75169501e/Go.gitignore
+# If you prefer the allow list template instead of the deny list, see community template:
+# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
+#
+# Binaries for programs and plugins
+*.exe
+*.exe~
+*.dll
+*.so
+*.dylib
+
+# Test binary, built with `go test -c`
+*.test
+
+# Code coverage profiles and other test artifacts
+*.out
+coverage.*
+*.coverprofile
+profile.cov
+
+# Dependency directories (remove the comment below to include it)
+# vendor/
+
+# Go workspace file
+go.work
+go.work.sum
+
+# env file
+.env
+
+# Editor/IDE
+# .idea/
+# .vscode/
diff --git a/go.mod b/go.mod
new file mode 100644
index 0000000..5a2115c
--- /dev/null
+++ b/go.mod
@@ -0,0 +1,5 @@
+module localhost/demo
+
+go 1.24.10
+
+require golang.org/x/exp v0.0.0-20251125195548-87e1e737ad39
diff --git a/go.sum b/go.sum
new file mode 100644
index 0000000..a3d0f52
--- /dev/null
+++ b/go.sum
@@ -0,0 +1,2 @@
+golang.org/x/exp v0.0.0-20251125195548-87e1e737ad39 h1:DHNhtq3sNNzrvduZZIiFyXWOL9IWaDPHqTnLJp+rCBY=
+golang.org/x/exp v0.0.0-20251125195548-87e1e737ad39/go.mod h1:46edojNIoXTNOhySWIWdix628clX9ODXwPsQuG6hsK0=
diff --git a/main.go b/main.go
new file mode 100644
index 0000000..947d028
--- /dev/null
+++ b/main.go
@@ -0,0 +1,11 @@
+package demo
+
+import "golang.org/x/exp/constraints"
+
+type Number interface {
+ constraints.Integer | constraints.Float
+}
+
+func Add[T Number](a, b T) T {
+ return a + b
+}
diff --git a/main_test.go b/main_test.go
new file mode 100644
index 0000000..ec1f286
--- /dev/null
+++ b/main_test.go
@@ -0,0 +1,33 @@
+package demo
+
+import "testing"
+
+func TestAddUint32(t *testing.T) {
+ var a uint32 = 2
+ var b uint32 = 3
+ t.Logf("a = %v %T, b = %v %T", a, a, b, b)
+ got := Add(a, b)
+ if got != 5 {
+ t.Errorf("Add(%v, %v) = %v; want 5", a, b, got)
+ }
+}
+
+func TestAddFloat32(t *testing.T) {
+ var a float32 = 2.0
+ var b float32 = 3.0
+ t.Logf("a = %v %T, b = %v %T", a, a, b, b)
+ got := Add(a, b)
+ if got != 5 {
+ t.Errorf("Add(%v, %v) = %v; want 5", a, b, got)
+ }
+}
+
+func TestAddInt64(t *testing.T) {
+ var a int64 = -2
+ var b int64 = -3
+ t.Logf("a = %v %T, b = %v %T", a, a, b, b)
+ got := Add(a, b)
+ if got != -5 {
+ t.Errorf("Add(%v, %v) = %v; want 5", a, b, got)
+ }
+}