2019年7月12日金曜日

開発環境

Head First Go (Jay McGavren(著)、O'Reilly Media)のChapter 3(call me - Functions)、Exercise(89)の解答を求めてみる。

コード

sample1_test.go

package main

import "testing"

func TestFunctionInt(t *testing.T) {
 gots := []int{
  functionA(2, 3),
  functionB(2, 3),
  functionA(5, 6),
  functionB(5, 6),
 }
 wants := []int{5, 6, 11, 30}
 for i, got := range gots {
  want := wants[i]
  if got != want {
   t.Errorf("got: %d, want: %d", got, want)
  }
 }
}

func TestFunctionBool(t *testing.T) {
 gots := []bool{
  functionC(true),
  functionC(false),
 }
 wants := []bool{false, true}
 for i, got := range gots {
  want := wants[i]
  if got != want {
   t.Errorf("got: %v, want: %v", got, want)
  }
 }
}

sample1.go

package main

import "fmt"

func functionA(a int, b int) int {
 return a + b
}
func functionB(a int, b int) int {
 return a * b
}
func functionC(a bool) bool {
 return !a
}
func functionD(a string, b int) {
 for i := 0; i < b; i++ {
  fmt.Print(a)
 }
 fmt.Println()
}

func main() {
 // $$$$
 functionD("$", 4)
 // hahaha
 functionD("ha", 3)

}

入出力結果(Bash、cmd.exe(コマンドプロンプト)、Terminal)

$ go test
PASS
ok   _/.../sample1 0.005s
$ go run sample1.go
$$$$
hahaha
$ 

0 コメント:

コメントを投稿