Home >Backend Development >Golang >Why is my function in a separate Go package undefined?
Function in Separate Package Undefined
In the given code structure, a function defined in a separate lib.go file is not recognized as defined when called from the main.go file. Let's explore the issue and find a solution.
When attempting to compile main.go with go build main.go, the error "undefined: Test" is encountered. This is because the Test function is defined in lib.go, which is in a different package (named "main") than main.go.
In Go, packages are isolated namespaces, and functions defined in one package cannot be directly accessed by functions in another package without explicit imports. To resolve this issue, you can do either of the following:
The above is the detailed content of Why is my function in a separate Go package undefined?. For more information, please follow other related articles on the PHP Chinese website!