Compare commits
5 Commits
39be6e1d4d
...
3569bcd485
Author | SHA1 | Date | |
---|---|---|---|
3569bcd485 | |||
cb26678c66 | |||
c37e1c26e1 | |||
323f4aa606 | |||
619c6b215e |
9
arrays/sum.go
Normal file
9
arrays/sum.go
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
func Sum(numbers []int) int {
|
||||||
|
sum := 0
|
||||||
|
for _, n := range numbers {
|
||||||
|
sum += n
|
||||||
|
}
|
||||||
|
return sum
|
||||||
|
}
|
29
arrays/sum_test.go
Normal file
29
arrays/sum_test.go
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestSum(t *testing.T) {
|
||||||
|
|
||||||
|
t.Run("collection of 5 numbers", func(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)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("collection of any size", func(t *testing.T) {
|
||||||
|
numbers := []int{1, 2, 3}
|
||||||
|
|
||||||
|
got := Sum(numbers)
|
||||||
|
want := 6
|
||||||
|
|
||||||
|
if got != want {
|
||||||
|
t.Errorf("got %d want %d given, %v", got, want, numbers)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
}
|
33
hello/hello.go
Normal file
33
hello/hello.go
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
const spanish = "Spanish"
|
||||||
|
const french = "French"
|
||||||
|
|
||||||
|
const englishHelloPrefix = "Hello, "
|
||||||
|
const spanishHelloPrefix = "Hola, "
|
||||||
|
const frenchHelloPrefix = "Bonjour, "
|
||||||
|
|
||||||
|
func Hello(name string, language string) string {
|
||||||
|
if name == "" {
|
||||||
|
name = "World"
|
||||||
|
}
|
||||||
|
|
||||||
|
return greetingPrefix(language) + name
|
||||||
|
}
|
||||||
|
|
||||||
|
func greetingPrefix(language string) (prefix string) {
|
||||||
|
switch language {
|
||||||
|
case french:
|
||||||
|
prefix = frenchHelloPrefix
|
||||||
|
case spanish:
|
||||||
|
prefix = spanishHelloPrefix
|
||||||
|
default:
|
||||||
|
prefix = englishHelloPrefix
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
func main() {
|
||||||
|
fmt.Println(Hello("world", ""))
|
||||||
|
}
|
28
hello/hello_test.go
Normal file
28
hello/hello_test.go
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestHello(t *testing.T) {
|
||||||
|
assertCorrectMessage := func(t testing.TB, got, want string) {
|
||||||
|
t.Helper()
|
||||||
|
if got != want {
|
||||||
|
t.Errorf("got %q want %q", got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Run("saying hello to people", func(t *testing.T) {
|
||||||
|
got := Hello("Chris", "")
|
||||||
|
want := "Hello, Chris"
|
||||||
|
assertCorrectMessage(t, got, want)
|
||||||
|
})
|
||||||
|
t.Run("empty string defaults to 'World'", func(t *testing.T) {
|
||||||
|
got := Hello("", "")
|
||||||
|
want := "Hello, World"
|
||||||
|
assertCorrectMessage(t, got, want)
|
||||||
|
})
|
||||||
|
t.Run("in Spanish", func(t *testing.T) {
|
||||||
|
got := Hello("Elodie", "Spanish")
|
||||||
|
want := "Hola, Elodie"
|
||||||
|
assertCorrectMessage(t, got, want)
|
||||||
|
})
|
||||||
|
}
|
6
integers/adder.go
Normal file
6
integers/adder.go
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
package integers
|
||||||
|
|
||||||
|
// Add takes two integers and return the sum of them.
|
||||||
|
func Add(a, b int) int {
|
||||||
|
return a + b
|
||||||
|
}
|
21
integers/adder_test.go
Normal file
21
integers/adder_test.go
Normal 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 %v, got %v", expected, sum)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleAdd() {
|
||||||
|
sum := Add(1, 5)
|
||||||
|
fmt.Println(sum)
|
||||||
|
// Output: 6
|
||||||
|
}
|
16
iteration/repeat.go
Normal file
16
iteration/repeat.go
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
package iteration
|
||||||
|
|
||||||
|
import "strings"
|
||||||
|
|
||||||
|
func Repeat(character string, count int) string {
|
||||||
|
var repeated string
|
||||||
|
|
||||||
|
for i := 0; i < count; i++ {
|
||||||
|
repeated += character
|
||||||
|
}
|
||||||
|
return repeated
|
||||||
|
}
|
||||||
|
|
||||||
|
func StringsRepeat(s string, count int) string {
|
||||||
|
return strings.Repeat(s, count)
|
||||||
|
}
|
32
iteration/repeat_test.go
Normal file
32
iteration/repeat_test.go
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
package iteration
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestRepeat(t *testing.T) {
|
||||||
|
repeated := Repeat("a", 5)
|
||||||
|
expected := "aaaaa"
|
||||||
|
|
||||||
|
if repeated != expected {
|
||||||
|
t.Errorf("expected %v, got %v", expected, repeated)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkRepeat(b *testing.B) {
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
Repeat("a", 5)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func BenchmarkStringsRepeat(b *testing.B) {
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
StringsRepeat("a", 5)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleRepeat() {
|
||||||
|
data := Repeat("a", 5)
|
||||||
|
fmt.Println(data)
|
||||||
|
// Outputs: aaaaa
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user