learn-golang-with-tdd

[TOC]


Hello, World

What I learned

Few rules to writing tests

Didn't work

# installing local documentation
go install golang.org/x/tools/cmd/godoc@latest

# after installing this command still don't work
godoc -http :8000
# ERROR: 'godoc' command not found

Starting a module

create the hello module:

mkdir hello
cd hello
go mod init hello
# check if the "go.mod" file was created

hello.go:

package main

import "fmt"

// Hello() is a separated function
// to make it testable
func Hello() string {
  return "Hello, world"
}

func main() {
  fmt.Println(Hello())
}

hello_test.go:

package main

import "testing"

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

  if got != want {
    t.Errorf("got %q want %q", got, want)
  }
}

Run the test:

go test