我嘗試通過在命令提示符下的測試套件通(HELLO-world_test.go):未能作出測試套件通golang「的sayHello()作爲值」
package helloworld
import "testing"
func TestHelloWorld(t *testing.T) {
output := sayHello()
if "Hello, World!" != output {
t.Fatalf("output: %s\nexpected: Hello,World!", output)
}
}
我的代碼如下(helloworld.go):
package helloworld
import "fmt"
func sayHello() {
fmt.Println("Hello, World!")
}
,並通過命令提示錯誤代碼是:
C:\Users\Troy\exercism\go\hello-world\go test -v
# _/C_/Users/Troy/exercism/go/hello-world
.\hello-world_test.go:6: sayHello() used as value
FAIL _/C_/Users/Troy/exercism/go/hello-world [build failed]
'sayHello'不會返回任何東西,但您將其輸出捕獲到'output'。 – akonsu 2015-02-12 01:24:17
您需要捕獲sdout並將捕獲的輸出與「Hello,World!」進行比較。有關如何捕獲stdout的示例,請參閱[示例運行器](https://golang.org/src/testing/example.go#L44)。 – 2015-02-12 01:35:31