dongqu7778 2016-01-30 07:51
浏览 447
已采纳

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

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
", test1)
    fmt.Printf("Integer from pack1 : %d
", 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 ?

  • 写回答

2条回答 默认 最新

  • dongtiran7769 2016-01-30 08:20
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog