Home > Backend Development > Golang > Why Does `go install` Fail with 'local import in non-local package' and How Can I Fix It?

Why Does `go install` Fail with 'local import in non-local package' and How Can I Fix It?

Patricia Arquette
Release: 2024-12-30 02:40:11
Original
284 people have browsed it

Why Does `go install` Fail with

Local Import Issue in Non-Local Package

When attempting to install a package (jacob.uk.com) from outside its directory (src/jacob.uk.com) using go install, a developer encountered the error "local import ./greeting in non-local package." This error occurs because local imports (i.e., relative imports within a package) are not allowed when specifying a non-local package for installation.

Solution:

To resolve this issue, the developer has two options:

  1. Change the working directory to src/jacob.uk.com and then execute go install (without specifying the package). This will allow the local import to function correctly. However, any unused imports in helloworld.go will still result in compile errors.
  2. Use absolute imports instead of local imports. Replace the local import statement (import "./greeting") with the absolute import statement (import "jacob.uk.com/greeting"). This will allow the package to be compiled, run, and installed from any directory. This is the preferred approach as it avoids the need for local imports and ensures consistency across different working directories.

The above is the detailed content of Why Does `go install` Fail with 'local import in non-local package' and How Can I Fix It?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template