普通网友 2015-01-26 15:27
浏览 275
已采纳

golang在另一个文件中声明的“未定义”功能?

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

Here is the source code:

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!"
}
  • 写回答

7条回答 默认 最新

  • doushanlv5184 2015-01-26 15:46
    关注

    Please read "How to Write Go Code".

    Don't use /src in your GOPATH. Packages are located in $GOPATH/src.

    For build or install you need to have your files in a package directory.

    For go run, you need to supply all files as argument:

    go run main.go employee.go
    

    But, you should almost always use go install, or go build (and preferably the former, as go build causes confusion when working with non-main packages)

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

报告相同问题?

悬赏问题

  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样