亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

無法在 Golang 中導入本地模塊

無法在 Golang 中導入本地模塊

Go
LEATH 2023-08-14 17:49:58
我正在嘗試導入本地模塊,但無法使用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 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我還得到You is not in a module也不在你的 GOPATH 中。有關如何設置 Go 項目的信息,請參閱https://github.com/golang/go/wiki/Modules 。VScode 中發出警告,即使我已GO111MODULE=on在~/.bashrc文件中設置
查看完整描述

3 回答

?
aluckdog

TA貢獻1847條經驗 獲得超7個贊

我知道三種方法:

  • 方法一(最好的方法):

# 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(對于不耐煩的人來說非??焖俚钠平夥椒ǎ?/p>


關閉 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 之外無法工作)


查看完整回答
反對 回復 2023-08-14
?
鳳凰求蠱

TA貢獻1825條經驗 獲得超4個贊

如果要導入本地模塊,則需要映射模塊路徑,以便它可以在本地文件系統中找到代碼。

首先使用 go mod edit 命令將模塊的任何導入替換為本地文件

$?go?mod?edit?-replace?example.com/greetings=../greetings

該命令指定 example.com/greetings 應替換為 ../greetings 以定位依賴項。運行命令后,當前目錄中的 go.mod 文件應在其 mod 文件中包含替換指令

之后使用 go mod tidy 命令來同步依賴項,添加您導入但尚未被當前模塊跟蹤的代碼所需的依賴項

$?go?mod?tidy


查看完整回答
反對 回復 2023-08-14
?
Helenr

TA貢獻1780條經驗 獲得超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 后應該生成一個文件

  • go.mod

  • 項目清單


查看完整回答
反對 回復 2023-08-14
  • 3 回答
  • 0 關注
  • 228 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號