第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

無法在 Golang 中導(dǎo)入本地模塊

無法在 Golang 中導(dǎo)入本地模塊

Go
一只萌萌小番薯 2022-05-23 15:07:18
我正在嘗試導(dǎo)入本地模塊,但無法使用go mod. 我最初使用go mod int github.com/AP/Ch2-GOMS請(qǐng)注意,我的環(huán)境是go1.14并且我使用 VSCode 作為我的編輯器。這是我的文件夾結(jié)構(gòu)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)} 我看不到本地模塊的自動(dòng)完成功能,例如handlers.NewHello.go build生成的go.mod內(nèi)容:module github.com/AP/Ch2-GOMSgo 1.14我也得到你既不在模塊中也不在你的 GOPATH 中。有關(guān)如何設(shè)置 Go 項(xiàng)目的信息,請(qǐng)參閱https://github.com/golang/go/wiki/Modules 。VScode中的警告,即使我已經(jīng)GO111MODULE=on在我的~/.bashrc文件中設(shè)置
查看完整描述

3 回答

?
Smart貓小萌

TA貢獻(xiàn)1911條經(jīng)驗(yàn) 獲得超7個(gè)贊

閱讀:Ian Lance Taylor 的評(píng)論(Go 的核心團(tuán)隊(duì))


我知道三種方法:


方法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"

你一定做錯(cuò)了什么,這就是為什么它不起作用。


方法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(急躁的快速破解方法):


關(guān)閉 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

原因:在構(gòu)建過程中,編譯器首先查找供應(yīng)商,然后查找 GOPATH,然后查找 GOROOT。因此,由于符號(hào)鏈接,VSCode 的 go 相關(guān)工具也將由于提供的符號(hào)鏈接而正常工作,因?yàn)樗蕾囉?GOPATH(它們?cè)?GOPATH 之外無法工作)


查看完整回答
反對(duì) 回復(fù) 2022-05-23
?
森林海

TA貢獻(xiàn)2011條經(jīng)驗(yàn) 獲得超2個(gè)贊

go mod tidy單獨(dú)在根文件夾為我做了



查看完整回答
反對(duì) 回復(fù) 2022-05-23
?
慕的地8271018

TA貢獻(xiàn)1796條經(jīng)驗(yàn) 獲得超4個(gè)贊

以下是步驟-


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

在上述步驟之前,您的項(xiàng)目路徑應(yīng)該是


project path = GOPATH/src/<project_name>

除了應(yīng)該還有 2 個(gè)與src文件夾平行的文件夾

  • 源代碼

每當(dāng)您安裝任何軟件包時(shí),它都應(yīng)該放在 pkg 文件夾中,并且在執(zhí)行 go mod tidy 之后應(yīng)該生成一個(gè)文件

  • 去.mod

  • 項(xiàng)目清單


查看完整回答
反對(duì) 回復(fù) 2022-05-23
  • 3 回答
  • 0 關(guān)注
  • 386 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)