Home > Backend Development > Golang > Why Does 'go install' Fail with 'local import' Errors for Non-Local Go Packages?

Why Does 'go install' Fail with 'local import' Errors for Non-Local Go Packages?

Barbara Streisand
Release: 2024-12-24 20:05:12
Original
609 people have browsed it

Why Does

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
    Copy after login
  • GOPATH: /Users/clarkj84/Desktop/LearningGo

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:

  1. Change the working directory to src/jacob.uk.com and execute go install without specifying the package.
  2. Use an absolute import path: import "jacob.uk.com/greeting"

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!

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