Local Import in Non-local Package in Go
When working with a specific file structure, it is important to understand the implications of using local imports within non-local packages.
Consider the following scenario:
File structure:
. ├── bin │ └── hello ├── pkg └── src └── jacob.uk.com ├── greeting │ └── greeting.go └── helloworld.go
When executing the command /usr/local/go/bin/go install jacob.uk.com from the src folder, an error may be encountered: local import "./greeting" in non-local package.
Explanation:
This error occurs because local imports (e.g., ./greeting) are not allowed when specifying a non-local package to install (jacob.uk.com).
Solution:
To resolve this issue, you can either:
Best Practice:
It is recommended to avoid using local imports in general. Instead, use absolute import paths to ensure portability and avoid potential errors.
The above is the detailed content of Why Does 'go install' Fail with 'local import' Errors for Non-Local Go Packages?. For more information, please follow other related articles on the PHP Chinese website!