douyi6818 2019-02-08 11:02
浏览 84
已采纳

进行测试<功能>返回未定义:<功能>

Trying to run "go test sum_test.go" returns an error:

./sum_test.go:18:13: undefined: SumInt8 FAIL command-line-arguments [build failed]

I'm taking an introductory course in golang. Our teacher distributed a code-file, sum.go, and a testing-file, sum_test.go. Trying to run "go test" on sum_test.go returns the error above. The code runs fine on our teachers mac, and he's having difficulties recreating the problem. Here's my go environment settings: https://pastebin.com/HcuNVcAF

sum.go

package sum

func SumInt8(a, b int8) int8 {
return a + b
}

func SumFloat64(a, b float64) float64 {
return a + b
}

sum_test.go

package sum

import "testing"

// Check https://golang.org/ref/spec#Numeric_types and stress the 
limits!
var sum_tests_int8 = []struct {
n1       int8
n2       int8
expected int8
}{
{1, 2, 3},
{4, 5, 9},
{120, 1, 121},
}

func TestSumInt8(t *testing.T) {
for _, v := range sum_tests_int8 {
    if val := SumInt8(v.n1, v.n2); val != v.expected {
        t.Errorf("Sum(%d, %d) returned %d, expected %d", 
v.n1, v.n2, val, v.expected)
    }
}
}

I see no particular errors, so I expected "go test sum_test.go" to run, and succeed. However it seems it can't find the method SumInt8 in sum.go.

  • 写回答

1条回答 默认 最新

  • dsgoj7457 2019-02-08 12:19
    关注
    $ go help packages
    

    Many commands apply to a set of packages:

    go action [packages]
    

    Usually, [packages] is a list of import paths.

    An import path that is a rooted path or that begins with a . or .. element is interpreted as a file system path and denotes the package in that directory.

    Otherwise, the import path P denotes the package found in the directory DIR/src/P for some DIR listed in the GOPATH environment variable (For more details see: 'go help gopath').

    If no import paths are given, the action applies to the package in the current directory.

    As a special case, if the package list is a list of .go files from a single directory, the command is applied to a single synthesized package made up of exactly those files, ignoring any build constraints in those files and ignoring any other files in the directory.


    List all the files in the current directory used in the test:

    go test sum_test.go sum.go
    

    or simply test the complete package in the current directory.

    go test
    

    For example,

    $ go test -v sum_test.go sum.go
    === RUN   TestSumInt8
    --- PASS: TestSumInt8 (0.00s)
    PASS
    ok      command-line-arguments  0.002s
    $ 
    

    or, for the complete package

    $ go test -v
    === RUN   TestSumInt8
    --- PASS: TestSumInt8 (0.00s)
    PASS
    ok      so/sum  0.002s
    $
    

    Option '-v' produces verbose output.

    sum_test.go:

    package sum
    
    import "testing"
    
    // Check https://golang.org/ref/spec#Numeric_types and stress the limits!
    var sum_tests_int8 = []struct {
        n1       int8
        n2       int8
        expected int8
    }{
        {1, 2, 3},
        {4, 5, 9},
        {120, 1, 121},
    }
    
    func TestSumInt8(t *testing.T) {
        for _, v := range sum_tests_int8 {
            if val := SumInt8(v.n1, v.n2); val != v.expected {
                t.Errorf("Sum(%d, %d) returned %d, expected %d",
                    v.n1, v.n2, val, v.expected)
            }
        }
    }
    

    sum.go:

    package sum
    
    func SumInt8(a, b int8) int8 {
        return a + b
    }
    
    func SumFloat64(a, b float64) float64 {
        return a + b
    }
    

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab