为了账号安全,请及时绑定邮箱和手机立即绑定

无法在 Golang 中导入本地模块

无法在 Golang 中导入本地模块

Go
一只萌萌小番薯 2022-05-23 15:07:18
我正在尝试导入本地模块,但无法使用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 mainimport (    "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-GOMSgo 1.14我也得到你既不在模块中也不在你的 GOPATH 中。有关如何设置 Go 项目的信息,请参阅https://github.com/golang/go/wiki/Modules 。VScode中的警告,即使我已经GO111MODULE=on在我的~/.bashrc文件中设置
查看完整描述

3 回答

?
Smart猫小萌

TA贡献1911条经验 获得超7个赞

阅读:Ian Lance Taylor 的评论(Go 的核心团队)


我知道三种方法:


方法1(最好的方法):

# 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"

你一定做错了什么,这就是为什么它不起作用。


方法2(好方法):

# 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

方法3(急躁的快速破解方法):


关闭 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

原因:在构建过程中,编译器首先查找供应商,然后查找 GOPATH,然后查找 GOROOT。因此,由于符号链接,VSCode 的 go 相关工具也将由于提供的符号链接而正常工作,因为它依赖于 GOPATH(它们在 GOPATH 之外无法工作)


查看完整回答
反对 回复 2022-05-23
?
森林海

TA贡献2011条经验 获得超2个赞

go mod tidy单独在根文件夹为我做了



查看完整回答
反对 回复 2022-05-23
?
慕的地8271018

TA贡献1796条经验 获得超4个赞

以下是步骤-


on main folder - go mod init

2.go mod tidy

3.go to the folder where main file is present

4.install the package via 

    go get <package name>

5.go build

在上述步骤之前,您的项目路径应该是


project path = GOPATH/src/<project_name>

除了应该还有 2 个与src文件夹平行的文件夹

  • 源代码

每当您安装任何软件包时,它都应该放在 pkg 文件夹中,并且在执行 go mod tidy 之后应该生成一个文件

  • 去.mod

  • 项目清单


查看完整回答
反对 回复 2022-05-23
  • 3 回答
  • 0 关注
  • 358 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号