gpt4 book ai didi

go - 无法在 Golang 中导入本地模块

转载 作者:行者123 更新时间:2023-12-01 22:15:24 25 4
gpt4 key购买 nike

我正在尝试导入本地模块,但无法使用 go mod 导入它.我最初使用 go mod int github.com/AP/Ch2-GOMS 构建我的项目

注意我的环境是go1.14我正在使用 VSCode 作为我的编辑器。

这是我的文件夹结构

Ch2-GOMS
│ ├── go.mod
│ ├── handlers
│ │ └── hello.go
│ └── main.go

我的 main.go代码:
package main

import (
"log"
"net/http"
"os"

"github.com/AP/Ch2-GOMS/handlers" // This gives "could not import github.com/AP/Ch2-GOMS/handlers" lint error
)

func main() {

l := log.New(os.Stdout, "product-api", log.LstdFlags)
hh := handlers.NewHello(l)

sm := http.NewServeMux()
sm.Handle("/", hh)

http.ListenAndServe(":9090", nil)
}

我看不到本地模块的自动完成功能,例如 handlers.NewHello .
go build生成 go.mod内容:
module github.com/AP/Ch2-GOMS
go 1.14

我也收到了 您既不在模块中,也不在 GOPATH 中。请看 https://github.com/golang/go/wiki/Modules有关如何设置 Go 项目的信息。 VScode 中的警告,即使我设置了 GO111MODULE=on在我的 ~/.bashrc文件

最佳答案

Read: Ian Lance Taylor's comment (Go's Core Team)



我知道三种方法:
  • 方法一 (最佳方式):

  • # Inside
    # Ch2-GOMS
    # │ ├── go.mod
    # │ ├── handlers
    # │ │ └── hello.go
    # │ └── main.go

    # In Ch2-GOMS
    go mod init github.com/AP/Ch2-GOMS

    # In main.go
    # Add import "github.com/AP/Ch2-GOMS/handlers"
    # But, make sure:
    # handlers/hello.go has a package name "package handlers"

    你一定做错了什么,这就是为什么它不起作用。
  • 方法二 (好办法):

  • # Inside
    # Ch2-GOMS
    # │ ├── go.mod
    # │ ├── handlers
    # │ │ └── hello.go
    # │ └── main.go

    # Inside the handlers package
    cd Ch2-GOMS/handlers
    go mod init github.com/AP/Ch2-GOMS/handlers # Generates go.mod
    go build # Updates go.mod and go.sum

    # Change directory to top-level (Ch2-GOMS)
    cd ..
    go mod init github.com/AP/Ch2-GOMS # Skip if already done
    go build # Must fail for github.com/AP/Ch2-GOMS/handlers
    vi go.mod

    在 Ch2-GOMS/go.mod 中添加以下行:

    # Open go.mod for editing and add the below line at the bottom (Not inside require)
    replace github.com/AP/Ch2-GOMS/handlers => ./handlers

    # replace asks to replace the mentioned package with the path that you mentioned
    # so it won't further look packages elsewhere and would look inside that's handlers package located there itself
  • 方法三 (不耐烦的快速破解方法):
  • 关闭 Go 模块 GO111MODULE=off
  • 删除 go.mod文件

  • # Check: echo $GOPATH

    # If $GOPATH is set
    mkdir -p $GOPATH/src/github.com/AP/Ch2-GOMS
    cd $GOPATH/src/github.com/AP/Ch2-GOMS


    # If $GOPATH is unset
    mkdir -p ~/go/src/github.com/AP/Ch2-GOMS
    cd ~/go/src/github.com/AP/Ch2-GOMS

    # Now create a symbolic link
    ln -s <full path to your package> handlers

    Reason: During the build, the compiler first looks in vendor, then GOPATH, then GOROOT. So, due to the symlink, VSCode's go related tools will also work correctly due to the symlink provided as it relies on GOPATH (They don't work outside of GOPATH)

    关于go - 无法在 Golang 中导入本地模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60680470/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com