Aym*_*rif 23 import module go go-modules
我正在尝试导入本地模块,但无法使用go mod. 我最初使用构建我的项目go mod init github.com/AP/Ch2-GOMS
请注意,我的环境是go1.14,并且我使用 VSCode 作为编辑器。
这是我的文件夹结构
\nCh2-GOMS\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 go.mod\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 handlers\n\xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 hello.go\n\xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 main.go\nRun Code Online (Sandbox Code Playgroud)\n我的main.go代码:
package main\n\nimport (\n "log"\n "net/http"\n "os"\n\n "github.com/AP/Ch2-GOMS/handlers" // This gives "could not import github.com/AP/Ch2-GOMS/handlers" lint error\n)\n\nfunc main() {\n\n l := log.New(os.Stdout, "product-api", log.LstdFlags)\n hh := handlers.NewHello(l)\n\n sm := http.NewServeMux()\n sm.Handle("/", hh)\n\n http.ListenAndServe(":9090", nil)\n} \nRun Code Online (Sandbox Code Playgroud)\n我看不到本地模块的自动完成功能,例如handlers.NewHello.
go build生成的go.mod内容:
module github.com/AP/Ch2-GOMS\ngo 1.14\nRun Code Online (Sandbox Code Playgroud)\n我还得到You is not in a module也不在你的 GOPATH 中。有关如何设置 Go 项目的信息,请参阅https://github.com/golang/go/wiki/Modules 。VScode 中发出警告,即使我已GO111MODULE=on在~/.bashrc文件中设置
p9s*_*9sh 24
\n\n\n阅读:Ian Lance Taylor 的评论(Go 的核心团队)
\n
我知道三种方法:
\n\n# Inside\n# Ch2-GOMS\n# \xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 go.mod\n# \xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 handlers\n# \xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 hello.go\n# \xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 main.go\n\n# In Ch2-GOMS\ngo mod init github.com/AP/Ch2-GOMS\n\n# In main.go\n# Add import "github.com/AP/Ch2-GOMS/handlers"\n# But, make sure: \n# handlers/hello.go has a package name "package handlers"\nRun Code Online (Sandbox Code Playgroud)\n\n您一定做错了什么,这就是它不起作用的原因。
\n\n# Inside\n# Ch2-GOMS\n# \xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 go.mod\n# \xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 handlers\n# \xe2\x94\x82 \xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 hello.go\n# \xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 main.go\n\n# Inside the handlers package\ncd Ch2-GOMS/handlers\ngo mod init github.com/AP/Ch2-GOMS/handlers # Generates go.mod\ngo build # Updates go.mod and go.sum\n\n# Change directory to top-level (Ch2-GOMS)\ncd ..\ngo mod init github.com/AP/Ch2-GOMS # Skip if already done\ngo build # Must fail for github.com/AP/Ch2-GOMS/handlers\nvi go.mod\nRun Code Online (Sandbox Code Playgroud)\n\n在 Ch2-GOMS/go.mod 内添加以下行:
\n\n# Open go.mod for editing and add the below line at the bottom (Not inside require)\nreplace github.com/AP/Ch2-GOMS/handlers => ./handlers\n\n# replace asks to replace the mentioned package with the path that you mentioned\n# so it won\'t further look packages elsewhere and would look inside that\'s handlers package located there itself\nRun Code Online (Sandbox Code Playgroud)\n\n方法3(对于不耐烦的人来说非常快速的破解方法):
\n\nGO111MODULE=offgo.mod文件# Check: echo $GOPATH\n\n# If $GOPATH is set\nmkdir -p $GOPATH/src/github.com/AP/Ch2-GOMS\ncd $GOPATH/src/github.com/AP/Ch2-GOMS\n\n\n# If $GOPATH is unset\nmkdir -p ~/go/src/github.com/AP/Ch2-GOMS\ncd ~/go/src/github.com/AP/Ch2-GOMS\n\n# Now create a symbolic link\nln -s <full path to your package> handlers\nRun Code Online (Sandbox Code Playgroud)\n\n\n\n原因:在构建过程中,编译器首先查找供应商,然后查找 GOPATH,然后查找 GOROOT。因此,由于符号链接,VSCode 的 go 相关工具也将由于提供的符号链接而正常工作,因为它依赖于 GOPATH(它们在 GOPATH 之外无法工作)
\n
如果要导入本地模块,则需要映射模块路径,以便它可以在本地文件系统中找到代码。
首先使用 go mod edit 命令将模块的任何导入替换为本地文件
$ go mod edit -replace example.com/greetings=../greetings
Run Code Online (Sandbox Code Playgroud)
该命令指定 example.com/greetings 应替换为 ../greetings 以定位依赖项。运行命令后,当前目录中的 go.mod 文件应在其 mod 文件中包含替换指令
之后使用 go mod tidy 命令来同步依赖项,添加您导入但尚未被当前模块跟踪的代码所需的依赖项
$ go mod tidy
Run Code Online (Sandbox Code Playgroud)
参考官方文档