diff options
| -rw-r--r-- | .gitignore | 33 | ||||
| -rw-r--r-- | go.mod | 5 | ||||
| -rw-r--r-- | go.sum | 2 | ||||
| -rw-r--r-- | main.go | 11 | ||||
| -rw-r--r-- | main_test.go | 33 |
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/ @@ -0,0 +1,5 @@ +module localhost/demo + +go 1.24.10 + +require golang.org/x/exp v0.0.0-20251125195548-87e1e737ad39 @@ -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= @@ -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) + } +} |
