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!"
}
package x
, also your main package typically only contains a single file, then imports the other pieces it needs – Arbitressgo run
? (and see golang.org/doc/code.html) – Bosworthmain.go
andemployee.go
? Because all go files are supposed to have a package statement, and I don't see any in yours. See: golang.org/doc/code.html#PackageNames – Arbitressgo
tool. – Bosworthgo build
orgo build a.go b.go
– Guberniya