Go 疑難雜症匯總



1. revision v0.0.0: unknown revision v0.0.0

go get -u github.com/uudashr/gopkgs/cmd/gopkgs 報錯:

[root@lubanseven home]$ go get -u github.com/uudashr/gopkgs/cmd/gopkgs
go: downloading github.com/uudashr/gopkgs v1.3.2
go: downloading github.com/uudashr/gopkgs/cmd/gopkgs v0.0.0-20191024034442-58e9141cd7d6
go: downloading github.com/uudashr/gopkgs v2.0.1+incompatible
go: github.com/uudashr/gopkgs/cmd/gopkgs upgrade => v0.0.0-20191024034442-58e9141cd7d6
go get: github.com/uudashr/gopkgs/cmd/gopkgs@v0.0.0-20191024034442-58e9141cd7d6 requires
        github.com/uudashr/gopkgs/v2@v2.1.0 requires
        github.com/uudashr/gopkgs@v0.0.0: reading github.com/uudashr/gopkgs/go.mod at revision v0.0.0: unknown revision v0.0.0

根據 Cannot install gopkgs tool,換個 gopkgs 安裝:

[root@lubanseven delve]$ go get github.com/uudashr/gopkgs/v2/cmd/gopkgs
go: downloading github.com/uudashr/gopkgs/v2 v2.1.2
go: found github.com/uudashr/gopkgs/v2/cmd/gopkgs in github.com/uudashr/gopkgs/v2 v2.1.2
go: downloading github.com/karrick/godirwalk v1.12.0
go: downloading github.com/pkg/errors v0.8.1

2. invalid version: unknown revision

2.1 現象及解決方法

go run cmd/main.go 時報錯:

[root@lubanseven go]# go run svc/lubanseven/cmd/lubanseven/main.go
vendor/k8s.io/client-go/plugin/pkg/client/auth/exec/metrics.go:21:2: cannot find package "." in:
        /home/lubanseven/go/src/AANM/go/vendor/io/fs

查看項目 go.mod 文件發現文件定義版本(1.16) 和當前 go version 版本(1.15.15) 不一致。於是刪掉 go.mod 文件,重新 go mod init <project_name>,執行 go mod tidy 報錯:

go: github.com/hashicorp/vault@v1.9.1 requires
        github.com/hashicorp/vault/api/auth/approle@v0.0.0-00010101000000-000000000000: invalid version: unknown revision 000000000000

查閱文檔發現 go mod tidy 調用的是 go get,接着調用的是 git 下載 github 的包。於是使用 go get 命令手動下載 vault:

go get -u github.com/hashicorp/vault

報錯:

ssh:connect to host github.com port 22: Connection timed out.

ssh 連接的報錯,有可能 port 端口不是 22,也有可能是別的原因,這里並未深究,將下載方式從 ssh 切換到 https:

git config --global url.git@github.com:.insteadOf https://github.com/

接着執行 go get 手動下載 go: github.com/hashicorp/vault 繼續報錯:

Permission denied (publickey).
fatal: Could not read from remote repository.

提示 Permission denied,將 public key 添加到 github 上,繼續執行 go get -u,繼續報錯:

go: github.com/hashicorp/vault@v1.9.1 requires
        github.com/hashicorp/vault/api/auth/approle@v0.0.0-00010101000000-000000000000: invalid version: unknown revision 000000000000

看來路走錯了,接着搜文檔,發現和之前把 go.mod 刪掉有關系。之前的 go.mod 中有 replace 字段定義包版本的行為,現在新建的包並沒有 replace,也沒有 require。

將之前包版本的 require 復制到當前 go.mod(只復制了 require 並未復制 replace),接着執行 go mod tidy,下載完成。

3. cannot find package "." in:*******

執行 go run cmd/main.go 報錯:

[root@lubanseven go]# go run svc/lubanseven/cmd/lubanseven/main.go
vendor/k8s.io/client-go/plugin/pkg/client/auth/exec/metrics.go:21:2: cannot find package "." in:
        /home/lubanseven/go/src/AANM/go/vendor/io/fs

還是原來那個錯...

查閱文檔發現 GOPATH 包路徑不對,當前執行路徑是:

[root@lubanseven go]# pwd
/home/lubanseven/vsWorkspace/dev/go

// GOPATH 路徑
[root@lubanseven ~]# go env | grep -i path
GOPATH="/root/go"

於是按照文檔要求將代碼復制到 GOPATH 路徑下,挫是挫了點,但是此刻能運行已經什么都不在乎了。接着執行,還是報一樣的錯...

冷靜思考一番,還是從開始的 go.mod 入手,開始的 go.mod 定義了 go 版本是 1.16 這里我們換了版本出現了這一系列問題,會不會是 go 版本的問題呢?於是,將 go 版本從 1.15 換到 1.16,接着執行 go run cmd/main.go 執行成功...

4. fatal: unable to find remote helper for 'https'

go mod tidy 報錯:

[root@lubanseven home]$ go mod tidy
Gin imports
        github.com/gin-gonic/gin: git ls-remote -q origin in /root/go/pkg/mod/cache/vcs/a923aa3ae357f66c754ef34c3358c689f5d969293b012aef737373496ea3e                   ef3: exit status 128:
        fatal: unable to find remote helper for 'https'

根據 unable-to-find-remote-helper-for-https-during-git-clone 設置:

$ yum install curl-devel
$ # cd to wherever the source for git is
$ cd /usr/local/src/git-1.7.9  
$ ./configure
$ make
$ make install

由於沒找到 git configure 目錄,直接 yum remove git,然后 yum install git。接着執行 go mod tidy 報錯:

go: finding module for package github.com/gin-gonic/gin
Gin imports
        github.com/gin-gonic/gin: module github.com/gin-gonic/gin: Get "https://proxy.golang.org/github.com/gin-gonic/gin/@v/list": proxyconnect tcp:                    EOF

看報錯信息和代理有關系,重新配置代理,執行 go mod tidy 成功安裝 Gin。

5. Get "https://xxx": unexpected EOF

go get -v github.com/rogpeppe/godef 報錯:

go get github.com/rogpeppe/godef: module github.com/rogpeppe/godef: Get "https://goproxy.cn/github.com/rogpeppe/godef/@v/list": unexpected EOF

根據 go get not working with unexpected EOF 取消 https_proxy,重新 go get,成功!

注意:也要留意 http_proxy 的情況,配置 http_proxy 也會遇到此類問題。

6. fatal: unable to access xxx Encountered end of file

git clone https://github.com/go-delve/delve 通過 clone 的方式安裝 delve,報錯:

[root@lubanseven home]$ git clone https://github.com/go-delve/delve
Cloning into 'delve'...
fatal: unable to find remote helper for 'https'

出現了問題 4 一樣的報錯,但前面已經配置過了為什么還會報錯呢?和前面的處理應該沒關系。懷疑歸懷疑,按照問題 4 的解決方式重新走一遍,報同樣的錯。

根據 Unable to find remote helper for https 設置 PATH 環境變量:

[root@lubanseven home]$ PATH=$PATH:/usr/libexec/git-core
[root@lubanseven home]$ git clone https://github.com/go-delve/delve
Cloning into 'delve'...
fatal: unable to access 'https://github.com/go-delve/delve/': Encountered end of file

現在報 Encountered end of file 錯誤,根據 fatal: unable to access xxx Encountered end of file 設置 git config:

[root@lubanseven home]$ git config --global http.proxy
[root@lubanseven home]$ git config --global --unset http.proxy
[root@lubanseven home]$ git clone https://github.com/go-delve/delve
Cloning into 'delve'...
warning: You appear to have cloned an empty repository.

重新 clone 成功。

7. package embed is not in GOROOT (/usr/local/go/src/embed)

編譯項目代碼報錯:package embed is not in GOROOT (/usr/local/go/src/embed)。發現 embed 是 golang1.6 的內置函數,當前 golang 版本為 1.4。

遂嘗試升級 golang 版本,根據 升級 Golang 嘗試實現多版本 golang 管理。在 $GOPATH/pkg/mod/golang.org/dl/ 下找到對應
go 版本(如果沒有的話需要先下載: go get golang.org/dl/go<version> ),編譯 main.go 為對應版本號:

$ go build -o go1.16.4 main.go

執行 ./go1.16.4 download 下載 go 軟件包:

$ ./go1.16.4 download
go1.16.4: download failed: Head "https://dl.google.com/go/go1.16.4.linux-amd64.tar.gz": dial tcp 142.250.74.78:443: i/o timeout

遇到了 i/o timeout 問題,查看 go env proxy 是否正確設置:

$ go env | grep proxy -i
GONOPROXY=""
GOPROXY="https://goproxy.cn,direct"

$ go env | grep 111 -i
GO111MODULE="on"

正確配置,在看這里的 142.250.74.78 是什么地址呢?
通過 wget 直接下載 https://dl.google.com/go/go1.16.4.linux-amd64.tar.gz 看看:

$ wget https://dl.google.com/go/go1.16.4.linux-amd64.tar.gz
--2022-02-20 15:16:34--  https://dl.google.com/go/go1.16.4.linux-amd64.tar.gz
Resolving dl.google.com (dl.google.com)... 142.250.74.110, 2a00:1450:400f:802::200e
Connecting to dl.google.com (dl.google.com)|142.250.74.110|:443...

解析的是域名 dl.google.com 的 ip,眾所周知的原因訪問不了 google 的服務器。配置代理,繼續 wget:

$ export https_proxy=https://10.144.xxx.xxx:8080

$ wget https://dl.google.com/go/go1.16.4.linux-amd64.tar.gz
--2022-02-20 15:30:07--  https://dl.google.com/go/go1.16.4.linux-amd64.tar.gz
Connecting to 10.144.xxx.xxx:8080... connected.
Proxy request sent, awaiting response... 200 OK
Length: 129044044 (123M) [application/x-gzip]
Saving to: ‘go1.16.4.linux-amd64.tar.gz’

 0% [                                                                                                  ] 120,225     36.0KB/s  eta 68m 14s

可以訪問並下載!

理論上這么走通之后,可以實現多版本 go 管理了。不過這里后續沒有走這條路(主要想折騰下多版本 go...),而是將原來的 go 刪掉,重新安裝新版本 go。

新版本安裝完后,執行 go run main.go,程序成功運行。

8. Could not import Golang package

vscode 提示:Could not import Golang package,原因是前面在升級過程中將 GOPATH 替換了,導致 vscode原有窗口加載的 mod 找不到了。

解決方法是重啟 vscode,重新加載 workspace,GOPATH 找到,問題解決。

9. vs code remote connect

通過 vs code remote-ssh 遠程連接 server。

添加 keypair 到本地。ssh-keygen -t rsa -m PEM -f wenhu.pem 生成 pem 格式密鑰。
將以 RSA 開頭的密鑰拷到本地,並將 OPENSSL 的密碼添加到 authorized_keys。

remote-ssh 遠程連接成功。

還有種方式是將已經連接成功的 keypair 拷到 server 上,執行類似添加 authorized_keys 操作,連接。

參考

  1. 在解決問題過程中,參考了以下資料,效果不錯適合參考:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM