在同一个包中的函数未定义 [英] Function in same package undefined

查看:143
本文介绍了在同一个包中的函数未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目结构是这样的。

  packagetest / 
main.go
lib.go

main.go 中,我有这个代码。

 包主

导入fmt

func main (){
fmt.Println(来自main.go的Hello)
Test()
}

lib.go 中,我有这段代码。

 




$ b func Test(){
fmt.Println(This is the Test function在lib.go中)
}

当我尝试使用 go build main.go ,我得到 ./ main.go:7:undefined:Test 。这种结构化我的代码的方式是可能的吗?

试试运行 go build 。当你把它作为参数给它时,它不会查找其他的go文件。你也可以做去建立* .go


My project structure is like this.

packagetest/
    main.go
    lib.go

In main.go, I have this code.

package main

import "fmt"

func main() {
    fmt.Println("Hello from main.go.")
    Test()
}

While in lib.go, I have this code.

package main

import "fmt"

func Test() {
    fmt.Println("This is the Test function in lib.go.")
}

When I try to compile with go build main.go, I get ./main.go:7: undefined: Test. Is this way of structuring my code possible?

解决方案

Try running just go build. When you give it a go file as an argument, it will not look for other go files. You can also do go build *.go

这篇关于在同一个包中的函数未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆