Home >Backend Development >Golang >Why Does 'local import' Fail When Importing a Non-Local Go Package?
In Go, a local import is used to import a package from the same directory or a subdirectory of the current working directory. However, this technique cannot be applied when importing a non-local package, as evidenced by the error "local import "./greeting" in non-local package."
To comprehend the error, let's examine the file structure provided:
/Users/clarkj84/Desktop/LearningGo └── src └── jacob.uk.com ├── greeting │ └── greeting.go └── helloworld.go
The issue arises when trying to execute go install jacob.uk.com from within the src directory. This command attempts to install the jacob.uk.com package, which includes the ./greeting import in helloworld.go. However, Go interprets this as a local import since it is executed from the src directory, while the package itself is not located within src.
To resolve the error, two options are available:
import "jacob.uk.com/greeting"
This approach allows the package to be imported and used regardless of the working directory.
Absolute imports offer several advantages over local imports:
The above is the detailed content of Why Does 'local import' Fail When Importing a Non-Local Go Package?. For more information, please follow other related articles on the PHP Chinese website!