不要运行名为package_test.go的程序。

huangapple go评论95阅读模式
英文:

go not running program with name package_test.go

问题

我在_pack1_包下有以下代码,文件名为pack1.go

package pack1

var Pack1Int int = 42
var pack1Float = 3.14

func ReturnStr() string {
    return "Hello world!"
}

在主程序中有以下代码,文件名为package_test.go

package main

import (
    "fmt"
    "./pack1"
)

func main() {
    var test1 string
    test1 = pack1.ReturnStr()
    fmt.Printf("Return string from pack1 : %s\n", test1)
    fmt.Printf("Integer from pack1 : %d\n", pack1.Pack1Int)
}

当我尝试使用命令go run package_test.go运行时,我得到以下错误:

go run: cannot run *_test.go files (package_test.go)

但是,如果我将文件重命名为abc.go,那么我会得到正确的输出,即:

Return string from pack1 : Hello world!
Integer from pack1 : 42

我想知道为什么使用package_test.go作为文件名会出错。对于只有main包的代码,这个文件名是可以正常工作的。

这是Go语言的一个bug吗?还是我做错了什么?

英文:

I have following code under package pack1. Name of file is pack1.go

package pack1

var Pack1Int int = 42
var pack1Float = 3.14

func ReturnStr() string {
    return "Hello world!"
}

And following code in main program. Name of file is package_test.go

package main

import (
    "fmt"
    "./pack1"
)

func main() {
    var test1 string
    test1 = pack1.ReturnStr()
    fmt.Printf("Return string from pack1 : %s\n", test1)
    fmt.Printf("Integer from pack1 : %d\n", pack1.Pack1Int)
}

When I try to run it with command go run package_test.go I get following error:

go run: cannot run *_test.go files (package_test.go)

But if I rename file to abc.go then I am getting proper output i.e.

Return string from pack1 : Hello world!
Integer from pack1 : 42

I am curious about what is wrong with using package_test.go as file name.
For code with only main package this name is working fine.

Is this a bug in Go or I am doing something wrong ?

答案1

得分: 13

不是一个错误,而是设计如此。go run会检测到_test文件,并将其视为包的测试文件,测试文件将被编译为一个单独的包,然后与主测试二进制文件链接并运行。
建议将您的包文件放在GOPATH/src/PACK_NAME/目录下,然后使用go test运行您的*_test.go文件。

英文:

Not a bug, it's designed so. go run detects the _test files and consider them as test files for a package, test files will be compiled as a separate package, and then linked and run with the main test binary.
It's recommended to put your package file to GOPATH/src/PACK_NAME/, then run your *_test.go with go test.

答案2

得分: 10

你不能将程序文件命名为*_test.go,因为这是集成的Go 测试系统的一部分。

要编写一个新的测试套件,请创建一个文件,其名称以_test.go结尾,其中包含如此处所述的TestXxx函数。将文件放在与被测试的包相同的包中。该文件将在常规包构建时被排除在外,但在运行“go test”命令时将被包含。有关更多详细信息,请运行“go help test”和“go help testflag”。

只需将package_test.go重命名为packagetest.go即可。

英文:

You can't name your program files as *_test.go as this is part of integrated Go testing system

> To write a new test suite, create a file whose name ends _test.go that contains the TestXxx functions as described here. Put the file in the same package as the one being tested. The file will be excluded from regular package builds but will be included when the “go test” command is run. For more detail, run “go help test” and “go help testflag”.

Just rename package_test.go to packagetest.go

答案3

得分: 0

对于BASH

运行 go run PATH_TO_FILES/!(*_test).go

注意

如果在运行此命令时出现事件未找到错误,可能需要在bash终端中启用扩展的globbing。

运行 shopt -s extglob

在运行 go run PATH_TO_FILES/!(*_test).go 之后。


对于使用ZSH的用户:

setopt extendedglob # 获取有关globbing的帮助

接下来

foo*~*bar* # 匹配以foo开头但不包含bar的所有内容

所以对于我的情况,go run PATH_TO_FILES/*~*_test.go*

英文:

For BASH

Run go run PATH_TO_FILES/!(*_test).go

NOTE

If you get an event not found error when running this command, you probably need to enable extended globbing in your bash terminal.

Run shopt-s extglob

after you Run go run PATH_TO_FILES/!(*_test).go


For those using ZSH

setopt extendedglob # to get help regarding globbing

next

foo*~*bar* # match everything that starts with foo but doesn't contain bar

So with my case, go run PATH_TO_FILES/*~*_test.go*

答案4

得分: -1

如果你正在使用Linux:

运行以下命令:ls PATH_TO_FILES/*.go | grep -v _test.go

这对我有效。

英文:

if you are using linux:

go run `ls PATH_TO_FILES/*.go | grep -v _test.go`

it workes for me.

huangapple
  • 本文由 发表于 2016年1月30日 15:51:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/35099417.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定