2019年8月21日水曜日

開発環境

Head First Go (Jay McGavren(著)、O'Reilly Media)のChapter 15(responding to requests - Web Apps)、Pool Puzzle(439)の解答を求めてみる。

コード

sample2_test.go

package main

func ExampleCallFunction() {
 callFunction(functionA)
 // Output: function called
}
func ExampleCallTwice() {
 callTwice(functionA)
 // Output: function called
 // function called
}
func ExampleCallWithArguments() {
 callWithArguments(functionC)
 // Output: function called
 // This is sentence is false
}
func ExamplePrintReturnValue() {
 printReturnValue(functionB)
 // Output: function called
 // Returning from function
}

sample2.go

package main

import "fmt"

func callFunction(passedFunction func()) {
 passedFunction()
}
func callTwice(passedFunction func()) {
 passedFunction()
 passedFunction()
}
func callWithArguments(passedFunction func(string, bool)) {
 passedFunction("This is sentence is", false)
}

func printReturnValue(passedFunction func() string) {
 fmt.Println(passedFunction())
}
func functionA() {
 fmt.Println("function called")
}

func functionB() string {
 fmt.Println("function called")
 return "Returning from function"
}
func functionC(a string, b bool) {
 fmt.Println("function called")
 fmt.Println(a, b)
}

func main() {}

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

$ go test
# _/.../sample2 [_/.../sample2.test]
./sample2_test.go:4:2: undefined: callFunction
./sample2_test.go:4:15: undefined: functionA
FAIL _/.../sample2 [build failed]
$ go test
PASS
ok   _/.../sample2 0.005s
$ go test
# _/.../sample2 [_/.../sample2.test]
./sample2_test.go:6:2: undefined: callTwice
FAIL _/.../sample2 [build failed]
$ go test
--- FAIL: Example (0.00s)
got:
function called
function called
function called
want:
function called\nfunction called
FAIL
exit status 1
FAIL _/.../sample2 0.005s
$ go test
--- FAIL: Example (0.00s)
got:
function called
function called
function called
want:
function called
function called
FAIL
exit status 1
FAIL _/.../sample2 0.005s
$ go test
PASS
ok   _/.../sample2 0.005s
$ go test
# _/.../sample2 [_/.../sample2.test]
./sample2_test.go:13:2: undefined: callWithArguments
./sample2_test.go:13:20: undefined: functionC
FAIL _/.../sample2 [build failed]
$ go test
PASS
ok   _/.../sample2 0.005s
$ go test
# _/.../sample2 [_/.../sample2.test]
./sample2_test.go:18:2: undefined: printReturnValue
./sample2_test.go:18:19: undefined: functionB
FAIL _/.../sample2 [build failed]
$ go test
PASS
ok   _/.../sample2 0.005s
$ 

テストで改訂2版 みんなのGo言語で知ったTastable Examplesを使ってみた。

0 コメント:

コメントを投稿