Skip to content

Commit

Permalink
整理hello-world章节
Browse files Browse the repository at this point in the history
  • Loading branch information
william's mac committed Nov 15, 2019
1 parent 0d9516d commit 3cc3567
Show file tree
Hide file tree
Showing 13 changed files with 205 additions and 315 deletions.
10 changes: 1 addition & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,6 @@
<img src="red-green-blue-gophers-smaller.png" />
</p>

[Art by Denise](https://twitter.com/deniseyu21)

[![Build Status](https://travis-ci.org/quii/learn-go-with-tests.svg?branch=master)](https://travis-ci.org/quii/learn-go-with-tests)
[![Go Report Card](https://goreportcard.com/badge/github.com/quii/learn-go-with-tests)](https://goreportcard.com/report/github.com/quii/learn-go-with-tests)

- Formats: [Gitbook](https://quii.gitbook.io/learn-go-with-tests), [EPUB or PDF](https://github.com/quii/learn-go-with-tests/releases)
- Translations: [中文](https://studygolang.gitbook.io/learn-go-with-tests), [Português](https://larien.gitbook.io/aprenda-go-com-testes/)

## Why

- Explore the Go language by writing tests
Expand All @@ -24,7 +16,7 @@
### Go fundamentals

1. [Install Go](install-go.md) - Set up environment for productivity.
2. [Hello, world](hello-world.md) - Declaring variables, constants, if/else statements, switch, write your first go program and write your first test. Sub-test syntax and closures.
2. [Hello, world](hello-world.md) - 声明变量,常量,if/else 语句,switch 语句,编写第一个 go 程序和测试代码。子测试(subtests)语法和闭包。
3. [Integers](integers.md) - Further Explore function declaration syntax and learn new ways to improve the documentation of your code.
4. [Iteration](iteration.md) - Learn about `for` and benchmarking.
5. [Arrays and slices](arrays-and-slices.md) - Learn about arrays, slices, `len`, varargs, `range` and test coverage.
Expand Down
File renamed without changes.
364 changes: 131 additions & 233 deletions hello-world.md

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions hello-world/v2/hello_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import "testing"

func TestHello(t *testing.T) {
got := Hello()
want := "Hello, world"
expected := "Hello, world"

if got != want {
t.Errorf("got %q want %q", got, want)
if got != expected {
t.Errorf("got %q expected %q", got, expected)
}
}
8 changes: 4 additions & 4 deletions hello-world/v3/hello_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package main
import "testing"

func TestHello(t *testing.T) {
got := Hello("Chris")
want := "Hello, Chris"
got := Hello("Bobo")
expected := "Hello, Bobo"

if got != want {
t.Errorf("got %q want %q", got, want)
if got != expected {
t.Errorf("got %q expected %q", got, expected)
}
}
8 changes: 4 additions & 4 deletions hello-world/v4/hello_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package main
import "testing"

func TestHello(t *testing.T) {
got := Hello("Chris")
want := "Hello, Chris"
got := Hello("Bobo")
expected := "Hello, Bobo"

if got != want {
t.Errorf("got %q want %q", got, want)
if got != expected {
t.Errorf("got %q expected %q", got, expected)
}
}
16 changes: 8 additions & 8 deletions hello-world/v5/hello_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@ import "testing"

func TestHello(t *testing.T) {

assertCorrectMessage := func(t *testing.T, got, want string) {
assertCorrectMessage := func(t *testing.T, got, expected string) {
t.Helper()
if got != want {
t.Errorf("got %q want %q", got, want)
if got != expected {
t.Errorf("got %q expected %q", got, expected)
}
}

t.Run("saying hello to people", func(t *testing.T) {
got := Hello("Chris")
want := "Hello, Chris"
assertCorrectMessage(t, got, want)
got := Hello("Bobo")
expected := "Hello, Bobo"
assertCorrectMessage(t, got, expected)
})

t.Run("empty string defaults to 'world'", func(t *testing.T) {
got := Hello("")
want := "Hello, World"
assertCorrectMessage(t, got, want)
expected := "Hello, World1"
assertCorrectMessage(t, got, expected)
})

}
8 changes: 4 additions & 4 deletions hello-world/v6/hello.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package main

import "fmt"

const spanish = "Spanish"
const chinese = "Chinese"
const french = "French"
const englishHelloPrefix = "Hello, "
const spanishHelloPrefix = "Hola, "
const chineseHelloPrefix = "你好, "
const frenchHelloPrefix = "Bonjour, "

// Hello returns a personalised greeting in a given language
Expand All @@ -14,8 +14,8 @@ func Hello(name string, language string) string {
name = "World"
}

if language == spanish {
return spanishHelloPrefix + name
if language == chinese {
return chineseHelloPrefix + name
}

if language == french {
Expand Down
28 changes: 14 additions & 14 deletions hello-world/v6/hello_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,35 @@ import "testing"

func TestHello(t *testing.T) {

assertCorrectMessage := func(t *testing.T, got, want string) {
assertCorrectMessage := func(t *testing.T, got, expected string) {
t.Helper()
if got != want {
t.Errorf("got %q want %q", got, want)
if got != expected {
t.Errorf("got %q want %q", got, expected)
}
}

t.Run("to a person", func(t *testing.T) {
got := Hello("Chris", "")
want := "Hello, Chris"
assertCorrectMessage(t, got, want)
got := Hello("Bobo", "")
expected := "Hello, Bobo"
assertCorrectMessage(t, got, expected)
})

t.Run("empty string", func(t *testing.T) {
got := Hello("", "")
want := "Hello, World"
assertCorrectMessage(t, got, want)
expected := "Hello, World"
assertCorrectMessage(t, got, expected)
})

t.Run("in Spanish", func(t *testing.T) {
got := Hello("Elodie", spanish)
want := "Hola, Elodie"
assertCorrectMessage(t, got, want)
t.Run("in Chinese", func(t *testing.T) {
got := Hello("波波", chinese)
expected := "你好, 波波"
assertCorrectMessage(t, got, expected)
})

t.Run("in French", func(t *testing.T) {
got := Hello("Lauren", french)
want := "Bonjour, Lauren"
assertCorrectMessage(t, got, want)
expected := "Bonjour, Lauren"
assertCorrectMessage(t, got, expected)
})

}
8 changes: 4 additions & 4 deletions hello-world/v7/hello.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package main

import "fmt"

const spanish = "Spanish"
const chinese = "Chinese"
const french = "French"
const englishHelloPrefix = "Hello, "
const spanishHelloPrefix = "Hola, "
const chineseHelloPrefix = "你好, "
const frenchHelloPrefix = "Bonjour, "

// Hello returns a personalised greeting in a given language
Expand All @@ -19,8 +19,8 @@ func Hello(name string, language string) string {
switch language {
case french:
prefix = frenchHelloPrefix
case spanish:
prefix = spanishHelloPrefix
case chinese:
prefix = chineseHelloPrefix
}

return prefix + name
Expand Down
28 changes: 14 additions & 14 deletions hello-world/v7/hello_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,35 @@ import "testing"

func TestHello(t *testing.T) {

assertCorrectMessage := func(got, want string) {
assertCorrectMessage := func(got, expected string) {
t.Helper()
if got != want {
t.Errorf("got %q want %q", got, want)
if got != expected {
t.Errorf("got %q expected %q", got, expected)
}
}

t.Run("saying hello to people", func(t *testing.T) {
got := Hello("Chris", "")
want := "Hello, Chris"
assertCorrectMessage(got, want)
got := Hello("Bobo", "")
expected := "Hello, Bobo"
assertCorrectMessage(got, expected)
})

t.Run("say hello world when an empty string is supplied", func(t *testing.T) {
got := Hello("", "")
want := "Hello, World"
assertCorrectMessage(got, want)
expected := "Hello, World"
assertCorrectMessage(got, expected)
})

t.Run("say hello in Spanish", func(t *testing.T) {
got := Hello("Elodie", spanish)
want := "Hola, Elodie"
assertCorrectMessage(got, want)
t.Run("say hello in Chinese", func(t *testing.T) {
got := Hello("波波", chinese)
expected := "你好, 波波"
assertCorrectMessage(got, expected)
})

t.Run("say hello in French", func(t *testing.T) {
got := Hello("Lauren", french)
want := "Bonjour, Lauren"
assertCorrectMessage(got, want)
expected := "Bonjour, Lauren"
assertCorrectMessage(got, expected)
})

}
8 changes: 4 additions & 4 deletions hello-world/v8/hello.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package main

import "fmt"

const spanish = "Spanish"
const chinese = "Chinese"
const french = "French"
const englishHelloPrefix = "Hello, "
const spanishHelloPrefix = "Hola, "
const chineseHelloPrefix = "你好, "
const frenchHelloPrefix = "Bonjour, "

// Hello returns a personalised greeting in a given language
Expand All @@ -21,8 +21,8 @@ func greetingPrefix(language string) (prefix string) {
switch language {
case french:
prefix = frenchHelloPrefix
case spanish:
prefix = spanishHelloPrefix
case chinese:
prefix = chineseHelloPrefix
default:
prefix = englishHelloPrefix
}
Expand Down
28 changes: 14 additions & 14 deletions hello-world/v8/hello_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,35 @@ import "testing"

func TestHello(t *testing.T) {

assertCorrectMessage := func(t *testing.T, got, want string) {
assertCorrectMessage := func(t *testing.T, got, expected string) {
t.Helper()
if got != want {
t.Errorf("got %q want %q", got, want)
if got != expected {
t.Errorf("got %q expected %q", got, expected)
}
}

t.Run("to a person", func(t *testing.T) {
got := Hello("Chris", "")
want := "Hello, Chris"
assertCorrectMessage(t, got, want)
got := Hello("Bobo", "")
expected := "Hello, Bobo"
assertCorrectMessage(t, got, expected)
})

t.Run("empty string", func(t *testing.T) {
got := Hello("", "")
want := "Hello, World"
assertCorrectMessage(t, got, want)
expected := "Hello, World"
assertCorrectMessage(t, got, expected)
})

t.Run("in Spanish", func(t *testing.T) {
got := Hello("Elodie", spanish)
want := "Hola, Elodie"
assertCorrectMessage(t, got, want)
t.Run("in Chinese", func(t *testing.T) {
got := Hello("波波", chinese)
expected := "你好, 波波"
assertCorrectMessage(t, got, expected)
})

t.Run("in French", func(t *testing.T) {
got := Hello("Lauren", french)
want := "Bonjour, Lauren"
assertCorrectMessage(t, got, want)
expected := "Bonjour, Lauren"
assertCorrectMessage(t, got, expected)
})

}

0 comments on commit 3cc3567

Please sign in to comment.