当我运行`go test`时,为什么这个函数未定义?(Why is this function undefined when I run `go test`?)

编程入门 行业动态 更新时间:2024-10-25 01:21:20
当我运行`go test`时,为什么这个函数未定义?(Why is this function undefined when I run `go test`?)

这是代码

package main import "fmt" func Haha() { fmt.Println("in Haha") } func main() { }

a_test.go

package main_test import "testing" func TestA(t *testing.T) { Haha() }

go build工程。 但是当我运行~/gopath/src/zjk/misc$ go test -v 。 这是我得到的

# zjk/misc_test ./a_test.go:6: undefined: Haha FAIL zjk/misc [build failed]

Here is the code

a.go

package main import "fmt" func Haha() { fmt.Println("in Haha") } func main() { }

a_test.go

package main_test import "testing" func TestA(t *testing.T) { Haha() }

go build works. But when I run ~/gopath/src/zjk/misc$ go test -v. Here is what I get

# zjk/misc_test ./a_test.go:6: undefined: Haha FAIL zjk/misc [build failed]

最满意答案

因为你有不同的包裹

应该:

package main import "fmt" func Haha() { fmt.Println("in Haha") } func main() { }

a_test.go

package main import "testing" func TestA(t *testing.T) { Haha() }

输出:

# zjk/misc_test in Haha PASS ok github.com/qwertmax/so 0.009s

because you have different packages

should be:

a.go

package main import "fmt" func Haha() { fmt.Println("in Haha") } func main() { }

a_test.go

package main import "testing" func TestA(t *testing.T) { Haha() }

output:

# zjk/misc_test in Haha PASS ok github.com/qwertmax/so 0.009s

更多推荐

本文发布于:2023-08-04 14:28:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1417802.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:当我   函数   未定义   test   run

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!