普通网友 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条)

报告相同问题?

悬赏问题

  • ¥15 asp.textbox后台赋值前端不能显示什么原因
  • ¥15 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥15 DruidDataSource一直closing
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误