golang "undefined" function declared in another file?

Juan M :

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!"
}
JimB :

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)

Este artigo é coletado da Internet.

Se houver alguma infração, entre em [email protected] Delete.

editar em
0

deixe-me dizer algumas palavras

0comentários
loginDepois de participar da revisão

Artigos relacionados

golang "undefined" function declared in another file?

This.id returns undefined when inside a declared function

Why does the compiler complain about undefined reference to a constexpr function even though it is defined in another source file?

Why does the compiler complain about undefined reference to a constexpr function even though it is defined in another source file?

Golang return values of function as input arguments to another

"undefined reference to" one function in a C header but not another

In functional programming, can a function call another function that was declared outside of it's scope and not passed as a parameter?

"this" is undefined inside an anonymous function returned by another function React

Expand function() by another function from another source file

Rendering a component declared inside the render function of another component does not include changes in the componentDidMount method in REACT 17

How to use a virtual function from derived class, declared in another class with base class parameters?

I can't get the value of a variable declared from another function in angular

Function not declared when it is VBA

Check if function is declared recursive

Undefined Reference when Invoking Function of One Object from Another Object

reading function from another file into a button in python

Openpyxl - Use imported function in another file

Updating swiftui text view by a function in another file

Calling an assembly function from another file

Import function from another file in same folder

Unable to call function from another Python file

React Calling function from another file and useEffect

Add function to class from another file in Typescript

Call a function from another file in ReactJS

How to parse 'askopenfilename' file pathway into another function?

Is extern needed for function defined in another file?

Calling a function declared in a third part gradle file from within the buildscript construct

why can't I call a function from another function in python. function undefined error

Function that reads a file that's opened in another function is producing error