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!"
}
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)