Hello world done

drew/sql-it
Drew Bednar 11 months ago
parent 441b3a9d54
commit ca7d449b44

@ -1,12 +1,49 @@
// Provides a Hello world example with tests
package main
import "fmt"
import (
"fmt"
)
// Constants can be grouped in a block.
// Add a line between related consts for
// readbility
const (
french = "French"
spanish = "Spanish"
englishHelloPrefix = "Hello, "
spanishHelloPrefix = "Hola, "
frenchHelloPrefix = "Bonjour, "
)
// Hello prints the string "Hello, world"
func Hello(name string) string {
return "Hello, " + name
func Hello(name string, language string) string {
if name == "" {
name = "World"
}
return greetingPrefix(language) + name
}
// greetingPrefix Using a named return value will initialize a "zero value"
// variable which as you can see below can be assigned to.
// this will display in Go Doc for your function.
// Lower case letter functions are "private" functions. They are internal to implementation.
func greetingPrefix(language string) (prefix string) {
switch language {
case french:
prefix = frenchHelloPrefix
case spanish:
prefix = spanishHelloPrefix
default:
prefix = englishHelloPrefix
}
// we use names return value in function signature
return
}
func main() {
fmt.Println(Hello("world"))
fmt.Println(Hello("world", ""))
}

@ -3,9 +3,37 @@ package main
import "testing"
func TestHello(t *testing.T) {
got := Hello("Drew")
expected := "Hello, Drew"
t.Run("saying hello to people", func(t *testing.T) {
got := Hello("Drew", "")
expected := "Hello, Drew"
assertCorrectMessage(t, got, expected)
})
t.Run("say 'Hello, world' when an empty string is supplied", func(t *testing.T) {
got := Hello("", "")
expected := "Hello, World"
assertCorrectMessage(t, got, expected)
})
t.Run("in Spanish", func(t *testing.T) {
got := Hello("Elodie", "Spanish")
expected := "Hola, Elodie"
assertCorrectMessage(t, got, expected)
})
t.Run("in French", func(t *testing.T) {
got := Hello("Drew", "French")
expected := "Bonjour, Drew"
assertCorrectMessage(t, got, expected)
})
}
func assertCorrectMessage(t testing.TB, got, expected string) {
// Needed to tell the test suite that this method is a helper.
// By doing this when it fails the line number reported will be in
// our function call rather than inside our test helper.
t.Helper()
if got != expected {
t.Errorf("got %q, expected %q", got, expected)
}

Loading…
Cancel
Save