Initial commit.

This commit is contained in:
Dylan Baird 2025-02-01 21:18:14 -05:00
commit 1f85b0ddc3
13 changed files with 237 additions and 0 deletions

19
collections/sum.go Normal file
View file

@ -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
}

26
collections/sum_test.go Normal file
View file

@ -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)
}
}

3
go.mod Normal file
View file

@ -0,0 +1,3 @@
module learn-go-with-tests
go 1.22.7

25
hello-world/hello.go Normal file
View file

@ -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", ""))
}

32
hello-world/hello_test.go Normal file
View file

@ -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)
}
}

6
integers/adder.go Normal file
View file

@ -0,0 +1,6 @@
package integers
// Returns the sum of two integers.
func Add(x, y int) int {
return x + y
}

21
integers/adder_test.go Normal file
View file

@ -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
}

27
iteration/repeat_test.go Normal file
View file

@ -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
}

10
iteration/repeater.go Normal file
View file

@ -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
}

View file

@ -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
}

View file

@ -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)
}
}

10
shapes/shapes.go Normal file
View file

@ -0,0 +1,10 @@
package shapes
type Rectangle struct {
Width float64
Height float64
}
func Perimeter(rec Rectangle) float64 {
return 2 * (rec.Width + rec.Height)
}

14
shapes/shapes_test.go Normal file
View file

@ -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)
}
}