简体   繁体   English

从编译的二进制文件中获取 golang 版本

[英]Get golang version from compiled binary

Is there a way to get golang version from pkg/ or from compiled binary?有没有办法从pkg/或编译的二进制文件中获取 golang 版本? I want to automate removal of $GOPATH/pkg folder only when I change the golang version.我只想在更改 golang 版本时自动删除$GOPATH/pkg文件夹。

Nevermind, found the answer 没关系,找到了答案

[ `strings $pkg_a_file | grep 'go object' | head -n 1 | cut -f 5 -d ' '` != `go version | cut -f 3 -d ' '` ] && \     
rm -rf $GOPATH/pkg

strings $pkg_a_file | grep 'go object' | head -n 1 | cut -f 5 -d ' ' strings $pkg_a_file | grep 'go object' | head -n 1 | cut -f 5 -d ' ' part will show something like go1.6.2 strings $pkg_a_file | grep 'go object' | head -n 1 | cut -f 5 -d ' '部分将显示类似go1.6.2

pkg_a_file can be something like this: pkg_a_file可以是这样的:

PKG_OS_ARCH=`go version | cut -d ' ' -f 4 | tr '/' '_'`
pkg_a_file=$GOPATH/pkg/$PKG_OS_ARCH/gitlab.com/kokizzu/gokil/A.a

External reference: http://kokizzu.blogspot.co.id/2016/06/solution-for-golang-slow-compile.html 外部参考: http//kokizzu.blogspot.co.id/2016/06/solution-for-golang-slow-compile.html

使用1.9.2,grep命令是

strings $pkg_a_file | grep 'Go cmd/compile' 

I found the following method, to check the version of Go lib used to compile the binary我找到了以下方法,检查用于编译二进制文件的 Go lib 的版本

$ strings compiled_binary | grep '^go1'
go1.17.4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM