“未定义"函数在另一个文件中声明? [英] "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代码" .
在软件包目录中使用 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安装
或开始构建
.
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屋!
查看全文