前言
Go 版本 :
$ go version
go version go1.21.4 darwin/amd64
我想對 go 檔案進行反彙編,然後就報錯了:
$ go tool compile -S race.go
race.go:3:8: could not import sync (file not found)
我就驚訝了一下,標準庫怎麼還能找不到呢?難道是我 GOROOT 配置錯了?
發現了問題原因
原來是因為 Go 團隊認為 Go 安裝包越來越大,就從 1.20 開始發行的安裝包不再為 GOROOT 中的軟體包提供預編譯的.a 檔案了。
Go 1.20版本中,GOROOT 下的原始碼將像其他使用者包那樣在構建後被快取到本機 cache 中。此外,go install 也不會為 GOROOT 下的軟體包安裝 .a 檔案。
https://github.com/golang/go/issues/47257
如何解決?
可以自行安裝標準庫的包
export GODEBUG=installgoroot=all
go install -v std
執行上述命令後,可以在 $GOROOT/pkg
目錄下檢視到對應作業系統的目錄中包含了標準庫的.a 檔案
$ ls $GOROOT/pkg
darwin_amd64 include tool
$ ls darwin_amd64
archive context.a embed.a flag.a html internal math net.a plugin.a runtime sync text vendor
bufio.a crypto encoding fmt.a html.a io math.a os reflect runtime.a sync.a time
bytes.a crypto.a encoding.a go image io.a mime os.a reflect.a sort.a syscall.a time.a
compress database errors.a hash image.a log mime.a path regexp strconv.a testing unicode
container debug expvar.a hash.a index log.a net path.a regexp.a strings.a testing.a unicode.a
參考內容
可以看下下面兩個連結的內容。或者在命令列中看看 go help install
命令的內容
$ go help install
usage: go install [build flags] [packages]
Install compiles and installs the packages named by the import paths.
....
Before Go 1.20, the standard library was installed to
$GOROOT/pkg/$GOOS_$GOARCH.
Starting in Go 1.20, the standard library is built and cached but not installed.
Setting GODEBUG=installgoroot=all restores the use of
$GOROOT/pkg/$GOOS_$GOARCH.
https://stackoverflow.com/questions/77826479/how-do-i-precompile-the-golang-standard-library#:~:text=The standard library is not precompiled by default%2C,the default behavior. Check https%3A%2F%2Fgithub.com%2Fgolang%2Fgo%2Fissues%2F65204 for more details.
https://github.com/golang/go/issues/65204
驗證結果
go tool compile -S race.go
<unlinkable>.Race STEXT size=202 args=0x0 locals=0x30 funcid=0x0 align=0x0
0x0000 00000 (/Users/zly/workspace/go/src/github.com/xiaowuzai/go-example/mrace/race.go:5) TEXT <unlinkable>.Race(SB), ABIInternal, $48-0
0x0000 00000 (/Users/zly/workspace/go/src/github.com/xiaowuzai/go-example/mrace/race.go:5) CMPQ SP, 16(R14)
0x0004 00004 (/Users/zly/workspace/go/src/github.com/xiaowuzai/go-example/mrace/race.go:5) PCDATA $0, $-2
0x0004 00004 (/Users/zly/workspace/go/src/github.com/xiaowuzai/go-example/mrace/race.go:5) JLS 189
0x000a 00010 (/Users/zly/workspace/go/src/github.com/xiaowuzai/go-example/mrace/race.go:5) PCDATA $0, $-1
0x000a 00010 (/Users/zly/workspace/go/src/github.com/xiaowuzai/go-example/mrace/race.go:5) PUSHQ BP
0x000b 00011 (/Users/zly/workspace/go/src/github.com/xiaowuzai/go-example/mrace/race.go:5) MOVQ SP, BP
0x000e 00014 (/Users/zly/workspace/go/src/github.com/xiaowuzai/go-example/mrace/race.go:5) SUBQ $40, SP
0x0012 00018 (/Users/zly/workspace/go/src/github.com/xiaowuzai/go-example/mrace/race.go:5) FUNCDATA $0, gclocals·ykHN0vawYuq1dUW4zEe2gA==(SB)
0x0012 00018 (/Users/zly/workspace/go/src/github.com/xiaowuzai/go-example/mrace/race.go:5) FUNCDATA $1, gclocals·sQxO+jiYy+d9ldxoWSePwQ==(SB)
0x0012 00018 (/Users/zly/workspace/go/src/github.com/xiaowuzai/go-example/mrace/rac
可以了