commit 1f85b0ddc305394403ce8eb4ee3363031d71c7f2 Author: Dylan Baird Date: Sat Feb 1 21:18:14 2025 -0500 Initial commit. diff --git a/collections/sum.go b/collections/sum.go new file mode 100644 index 0000000..0167424 --- /dev/null +++ b/collections/sum.go @@ -0,0 +1,19 @@ +package collections + +func Sum(numbers []int) int { + sum := 0 + for _, number := range numbers { + sum += number + } + + return sum +} + +func SumAll(numberSlices ...[]int) []int { + var sums []int + for _, numbers := range numberSlices { + sums = append(sums, Sum(numbers)) + } + + return sums +} diff --git a/collections/sum_test.go b/collections/sum_test.go new file mode 100644 index 0000000..82b1869 --- /dev/null +++ b/collections/sum_test.go @@ -0,0 +1,26 @@ +package collections + +import ( + "reflect" + "testing" +) + +func TestSum(t *testing.T) { + numbers := []int{1, 2, 3, 4, 5} + + got := Sum(numbers) + want := 15 + + if got != want { + t.Errorf("got %d want %d given %v", got, want, numbers) + } +} + +func TestSumAll(t *testing.T) { + got := SumAll([]int{1, 2}, []int{1, 2, 3, 4, 5}) + want := []int{3, 15} + + if !reflect.DeepEqual(got, want) { + t.Errorf("got %d want %d", got, want) + } +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..5d03a94 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module learn-go-with-tests + +go 1.22.7 diff --git a/hello-world/hello.go b/hello-world/hello.go new file mode 100644 index 0000000..fef3c04 --- /dev/null +++ b/hello-world/hello.go @@ -0,0 +1,25 @@ +package main + +import "fmt" + +const spanish = "Spanish" +const spanishHelloPrefix = "Hola, " +const englishHelloPrefix = "Hello, " + +func Hello(name, language string) string { + if name == "" { + name = "world" + } + + prefix := englishHelloPrefix + switch language { + case spanish: + prefix = spanishHelloPrefix + } + + return prefix + name +} + +func main() { + fmt.Println(Hello("World", "")) +} diff --git a/hello-world/hello_test.go b/hello-world/hello_test.go new file mode 100644 index 0000000..999f718 --- /dev/null +++ b/hello-world/hello_test.go @@ -0,0 +1,32 @@ +package main + +import "testing" + +func TestHello(t *testing.T) { + t.Run("say hello to given name", func(t *testing.T) { + got := Hello("Judas", "") + want := "Hello, Judas" + + assertCorrectMessage(t, got, want) + }) + t.Run("hello world on empty string", func(t *testing.T) { + got := Hello("", "") + want := "Hello, world" + + assertCorrectMessage(t, got, want) + }) + t.Run("spanish", func(t *testing.T) { + got := Hello("Marjorie", "Spanish") + want := "Hola, Marjorie" + + assertCorrectMessage(t, got, want) + }) +} + +func assertCorrectMessage(t testing.TB, got, want string) { + t.Helper() + + if got != want { + t.Errorf("got %q want %q", got, want) + } +} diff --git a/integers/adder.go b/integers/adder.go new file mode 100644 index 0000000..b8c23bf --- /dev/null +++ b/integers/adder.go @@ -0,0 +1,6 @@ +package integers + +// Returns the sum of two integers. +func Add(x, y int) int { + return x + y +} diff --git a/integers/adder_test.go b/integers/adder_test.go new file mode 100644 index 0000000..521d24e --- /dev/null +++ b/integers/adder_test.go @@ -0,0 +1,21 @@ +package integers + +import ( + "fmt" + "testing" +) + +func TestAdder(t *testing.T) { + sum := Add(2, 2) + expected := 4 + + if sum != expected { + t.Errorf("expected %d got %d", expected, sum) + } +} + +func ExampleAdd() { + sum := Add(1, 5) + fmt.Println(sum) + // Output: 6 +} diff --git a/iteration/repeat_test.go b/iteration/repeat_test.go new file mode 100644 index 0000000..f98a601 --- /dev/null +++ b/iteration/repeat_test.go @@ -0,0 +1,27 @@ +package iteration + +import ( + "fmt" + "testing" +) + +func TestRepeat(t *testing.T) { + repeated := Repeat("a", 3) + expected := "aaa" + + if repeated != expected { + t.Errorf("expected %q got %q", expected, repeated) + } +} + +func BenchmarkRepeat(b *testing.B) { + for i := 0; i < b.N; i++ { + Repeat("a", 5) + } +} + +func ExampleRepeat() { + res := Repeat("a", 5) + fmt.Println(res) + // Output: aaaaa +} diff --git a/iteration/repeater.go b/iteration/repeater.go new file mode 100644 index 0000000..e43d038 --- /dev/null +++ b/iteration/repeater.go @@ -0,0 +1,10 @@ +package iteration + +func Repeat(char string, n int) string { + var repeated string + for i := 0; i < n; i++ { + repeated += char + } + + return repeated +} diff --git a/pointererror/pointererror.go b/pointererror/pointererror.go new file mode 100644 index 0000000..5570577 --- /dev/null +++ b/pointererror/pointererror.go @@ -0,0 +1,22 @@ +package pointererror + +import "fmt" + +type Bitcoin int + +func (b Bitcoin) String() string { + return fmt.Sprintf("%d BTC", b) +} + +type Wallet struct { + balance Bitcoin +} + +func (w *Wallet) Deposit(amount Bitcoin) { + w.balance += amount * 2 +} + +func (w *Wallet) Balance() Bitcoin { + fmt.Printf("address of balance in Balance is %p \n", &w.balance) + return w.balance +} diff --git a/pointererror/pointererror_test.go b/pointererror/pointererror_test.go new file mode 100644 index 0000000..838c1c9 --- /dev/null +++ b/pointererror/pointererror_test.go @@ -0,0 +1,22 @@ +package pointererror + +import ( + "fmt" + "testing" +) + +func TestWallet(t *testing.T) { + wallet := Wallet{} + + wallet.Deposit(Bitcoin(10)) + + got := wallet.Balance() + + fmt.Printf("address of balance in test is %p \n", &wallet.balance) + + want := Bitcoin(10) + + if got != want { + t.Errorf("got %s want %s", got, want) + } +} diff --git a/shapes/shapes.go b/shapes/shapes.go new file mode 100644 index 0000000..7544a20 --- /dev/null +++ b/shapes/shapes.go @@ -0,0 +1,10 @@ +package shapes + +type Rectangle struct { + Width float64 + Height float64 +} + +func Perimeter(rec Rectangle) float64 { + return 2 * (rec.Width + rec.Height) +} diff --git a/shapes/shapes_test.go b/shapes/shapes_test.go new file mode 100644 index 0000000..d059975 --- /dev/null +++ b/shapes/shapes_test.go @@ -0,0 +1,14 @@ +package shapes + +import "testing" + +func TestPerimeter(t *testing.T) { + rectangle := Rectangle{10.00, 10.00} + + got := Perimeter(rectangle) + want := 40.0 + + if got != want { + t.Errorf("got %.2f want %.2f", got, want) + } +}