Create a gist now

Instantly share code, notes, and snippets.

print go version from built binary
#!/bin/bash -e
# usage
# $0 path/to/go/built/binary
usage() {
echo "Usage"
echo " $0 path/to/go/built/binary"
exit 2
}
obj=$1
if [ -z $obj ] || [ ! -e $obj ];then
echo "No such file"
usage
fi
v=(`go tool nm -size $obj | grep 'runtime.buildVersion' | awk '{print $1,$2}'`)
addr=${v[0]}
size=${v[1]}
start=`objdump -s $obj | grep $addr | awk '{hex=$2; printf "0x%s%s%s%s\n",substr(hex,7,2),substr(hex,5,2),substr(hex,3,2),substr(hex,1,2);}'`
end=`printf "%d+%d\n" $start $size | bc`
objdump -s --start-address=$start --stop-address=$end $obj | grep go | sed -e 's/.*\(go.*\).*/\1/' -e 's/\.\.\+//g'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment