3 回答

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

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
項目清單
- 3 回答
- 0 關注
- 380 瀏覽
添加回答
舉報