“未定义"在另一个文件中声明的函数? [英] "undefined" function declared in another file?
本文介绍了“未定义"在另一个文件中声明的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试编写一个基本的 go 程序,它在不同的文件上调用一个函数,但它是同一个包的一部分.但是,它返回:
I'm trying to write a basic go program that calls a function on a different file, but a part of the same package. However, it returns:
undefined: NewEmployee
这是源代码:
main.go
:
package main
func main() {
emp := NewEmployee()
}
employee.go
:
package main
type Employee struct {
name string
age int
}
func NewEmployee() *Employee {
p := &Employee{}
return p
}
func PrintEmployee (p *Employee) {
return "Hello world!"
}
推荐答案
在包目录中使用 go build
或 go install
,或为包提供导入路径.不要对 build
或 install
使用文件参数.
Use go build
or go install
within the package directory, or supply an import path for the package. Do not use file arguments for build
or install
.
虽然您可以为 go run
使用文件参数,但您应该构建一个包,通常使用 go run .
,尽管您几乎总是应该使用 go安装
,或go build
.
While you can use file arguments for go run
, you should build a package instead, usually with go run .
, though you should almost always use go install
, or go build
.
这篇关于“未定义"在另一个文件中声明的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文