无法在 Golang 中导入本地模块

我正在尝试导入本地模块,但无法使用go mod. 我最初使用构建我的项目go mod init 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

我还得到You is not in a module也不在你的 GOPATH 中。有关如何设置 Go 项目的信息,请参阅https://github.com/golang/go/wiki/Modules 。VScode 中发出警告,即使我已GO111MODULE=on在~/.bashrc文件中设置


LEATH
浏览 136回答 3
3回答

aluckdog

我知道三种方法:方法一(最好的方法):# Inside# Ch2-GOMS# │   ├── go.mod# │   ├── handlers# │   │   └── hello.go# │   └── main.go# In Ch2-GOMSgo 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"您一定做错了什么,这就是它不起作用的原因。方法2(好方法):# Inside# Ch2-GOMS# │   ├── go.mod# │   ├── handlers# │   │   └── hello.go# │   └── main.go# Inside the handlers packagecd Ch2-GOMS/handlersgo mod init github.com/AP/Ch2-GOMS/handlers # Generates go.modgo 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 donego build # Must fail for github.com/AP/Ch2-GOMS/handlersvi 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方法3(对于不耐烦的人来说非常快速的破解方法):关闭 Go 模块GO111MODULE=off删除go.mod文件# Check: echo $GOPATH# If $GOPATH is setmkdir -p $GOPATH/src/github.com/AP/Ch2-GOMScd $GOPATH/src/github.com/AP/Ch2-GOMS# If $GOPATH is unsetmkdir -p ~/go/src/github.com/AP/Ch2-GOMScd ~/go/src/github.com/AP/Ch2-GOMS# Now create a symbolic linkln -s <full path to your package> handlers原因:在构建过程中,编译器首先查找供应商,然后查找 GOPATH,然后查找 GOROOT。因此,由于符号链接,VSCode 的 go 相关工具也将由于提供的符号链接而正常工作,因为它依赖于 GOPATH(它们在 GOPATH 之外无法工作)

凤凰求蛊

如果要导入本地模块,则需要映射模块路径,以便它可以在本地文件系统中找到代码。首先使用 go mod edit 命令将模块的任何导入替换为本地文件$ go mod edit -replace example.com/greetings=../greetings该命令指定 example.com/greetings 应替换为 ../greetings 以定位依赖项。运行命令后,当前目录中的 go.mod 文件应在其 mod 文件中包含替换指令之后使用 go mod tidy 命令来同步依赖项,添加您导入但尚未被当前模块跟踪的代码所需的依赖项$ go mod tidy

Helenr

以下是步骤-on main folder - go mod init2.go mod tidy3.go to the folder where main file is present4.install the package via&nbsp;&nbsp; &nbsp; go get <package name>5.go build在上述步骤之前,您的项目路径应该是project&nbsp;path&nbsp;=&nbsp;GOPATH/src/<project_name>另外应该还有 2 个与src文件夹平行的文件夹源代码包装垃圾桶当你安装任何软件包时,它应该进入 pkg 文件夹,并且在执行 go mod tidy 后应该生成一个文件go.mod项目清单
打开App,查看更多内容
随时随地看视频慕课网APP