惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Hacker News - Newest:
Hacker News - Newest: "LLM"
Webroot Blog
Webroot Blog
S
Security @ Cisco Blogs
H
Heimdal Security Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
www.infosecurity-magazine.com
www.infosecurity-magazine.com
N
News and Events Feed by Topic
H
Hacker News: Front Page
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Application and Cybersecurity Blog
Application and Cybersecurity Blog
SecWiki News
SecWiki News
N
News | PayPal Newsroom
T
Tor Project blog
W
WeLiveSecurity
A
Arctic Wolf
Security Archives - TechRepublic
Security Archives - TechRepublic
S
Secure Thoughts
月光博客
月光博客
AWS News Blog
AWS News Blog
D
Docker
C
CERT Recently Published Vulnerability Notes
MyScale Blog
MyScale Blog
Google Online Security Blog
Google Online Security Blog
大猫的无限游戏
大猫的无限游戏
T
The Blog of Author Tim Ferriss
I
InfoQ
人人都是产品经理
人人都是产品经理
Recent Announcements
Recent Announcements
Google DeepMind News
Google DeepMind News
Hacker News: Ask HN
Hacker News: Ask HN
Blog — PlanetScale
Blog — PlanetScale
博客园 - 【当耐特】
Engineering at Meta
Engineering at Meta
Stack Overflow Blog
Stack Overflow Blog
Recorded Future
Recorded Future
罗磊的独立博客
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
T
The Exploit Database - CXSecurity.com
D
DataBreaches.Net
S
Security Affairs
WordPress大学
WordPress大学
T
Threatpost
Microsoft Security Blog
Microsoft Security Blog
V
Vulnerabilities – Threatpost
The Hacker News
The Hacker News
S
SegmentFault 最新的问题
B
Blog RSS Feed
Project Zero
Project Zero
P
Proofpoint News Feed

文章列表 on polarisxu

通过一个例子让你彻底掌握 Go 工作区模式 这道题正确率竟然只有 22% Go 开源项目推荐:一个简单的 Go 练手项目 go test 如何禁用缓存? Go1.18 快讯:constraints 包被移除标准库 跟着 Go 作者学泛型 「2022 版」轻松搞定 Go 开发环境 鹅厂小哥用行动反对加班:程序员加班能被终结吗? Go 为什么选择 Gopher 作为吉祥物? 本地如何配置多个 GitHub/Gitee 账号? 为 Java 开发者准备的 Go 教程 02:Java 有而 Go 无 GitHub 发现了 studygolang 项目依赖的漏洞 Typora 宣布收费后,这款开源 Markdown 编辑器火了 为 Java 开发者准备的 Go 教程 01:漫游了 我的 2021 年终总结 泛型版 singleflight:Go 中如何防止缓存击穿? 2021 年你写了多少代码?这个 Go 工具帮你统计 Go1.18 新特性:TryLock Go 泛型入门教程 程序员瑞士军刀:各种结构的转换工具 终于有 Go 版的 Elasticsearch 了 Go泛型系列:maps 包讲解 Go 1.18 中的 any 是什么? Go1.18 快讯:新的 IP 包 Go编程模式:详解函数式选项模式 Go泛型系列:slices 包讲解 Nginx 竟然也有 playground — Go 语言构建的 Go泛型系列:Go1.18 类型约束那些事 Go1.18 快讯:Module 工作区模式 Go1.18 快讯:新增的 Cut 函数太方便了 Go1.18 快讯:废弃了这个 API Go1.18 快讯:新增字符串 Clone API Go:如何获得项目根目录? Go 如何获取和设置环境变量 重磅变动:更快找到微信公众号里的Go文章 Go 这样设置版本号:我们的项目也可以 假期结束,推荐 2 本 manning 出的 Go 图书 Go Fiber 框架系列教程 04:测试应用 Go Fiber 框架系列教程 03:中间件 Go泛型系列:提前掌握Go泛型的基本使用 推荐一个 Go GUI 实战项目 Go Fiber 框架系列教程 02:详解相关 API 的使用 Go Fiber 框架系列教程 01: 和 Express 对比学习 Go Module:私有不合规库怎么解决引用问题 Go1.17 新特性:go get 变了 Go1.17 新特性:testing 包的相关变化 StackOverflow 上关于 Go select 死锁的问题 新书推荐:用 Gin 框架构建分布式应用 Go 官网要变天。。。 Gin 这是要成为 Go 官方框架? 扬眉吐气:刚刚,Go 已经默认支持泛型了 Go 1.17 新特性:Module 有哪些变化? Go 第三方库推荐:类型转换如此简单 担心密码提交到 GitHub?建议使用这个 Go 开源工具 Go 的时间格式化为什么是 2006-01-02 15:04:05? GitHub 为 Go 社区带来安全支持 Go开源项目推荐:500行代码确认请求时间花在哪 一道关于 len 函数的诡异 Go 面试题解析 厉害了我的 Go:推荐系统都有开源实现 Go1.17 新特性:新版构建约束 这个工具真好:看看你的Go项目依赖有无漏洞 网友很强大,发现了Go并发下载的Bug Rust 劝退系列 09:函数 Go项目实战:一步步构建一个并发文件下载器 这本 Go 新书挺期待的:100 个常见错误 Go 启用新的官方问答社区 这是要干嘛?!微软招 Go 编译器全职开发人员 Go1.17 快报之标准库越来越注重易用性 Go1.17 新特性之切片变数组 回顾 Go 官网的演变史 站长8年前的Go代码竟然进入大厂的项目里了 Go图书翻译:一个好消息,一个坏消息 不怕烂尾!决定组织翻译这本 Go 图书 Rust 劝退系列 08:模式匹配 一本花了2.5年写成的Go免费在线图书 被黑惨了:一句话,说明自己会 Go,咋整? Go1.16 中的新函数 signal.NotifyContext 怎么用? Go 1.17 新特性提前学之测试执行随机化 Rust 劝退系列 07:流程控制 官方的 Go 多版本管理:使用和原理 Rust新书:给你一个劝退的理由 周刊题解:常量表达式这个规则应该了解下 盘点那些使用 Go 语言的国外公司 编写了50万行Go代码是一种什么体验 Rust 劝退系列 06:常量 Uber 使用 Go 的规模这么大?!都自己定制的 Go 编译器了 Go Team Leader — rsc 大神新开源了一个库,增强模板功能 再一次看到了 Go 的节制:int128 类型要不要支持? Rust 劝退系列 05:复合数据类型 「卷」有理论依据:海勒姆定律—Go又是怎么卷的 Go 真的也可以进行 GUI 开发:还有这样的图书呢 Rust 劝退系列 04:基本数据类型 我的 Go 语言书单 Rust 劝退系列 03:变量 我又来推荐免费 Go 新书了:一本用 Go 讲架构的书 注释竟然还有特殊用途?一文解惑 //go:linkname 指令 这个功能,公众号、微信群会不会被玩坏? 一道 Go 闭包题,面试官说原来自己答错了:面别人也涨知识 Rust 劝退系列 02:第一个 Rust 程序 Rust 劝退系列 01:打造开发环境
发现 go version 的一个另类用法:你肯定想不到
2021-03-19 · via 文章列表 on polarisxu

大家好,我是站长 polarisxu。

对于 go version,大家应该不陌生。在很多入门教程,安装 Go 后,一般会建议执行 go version 看看是否安装成功;亦或遇到问题,别人会问你 Go 哪个版本,你也会通过 go version 命令查看。所以,go version 的一个作用是查看本地使用的 Go 版本。

但实际上,go version 还有其他用途,甚至可以说,输出本地 Go 版本号只是它功能的一个特例。先 go help version 看看:

$ go help version
usage: go version [-m] [-v] [file ...]

Version prints the build information for Go executables.

Go version reports the Go version used to build each of the named
executable files.

If no files are named on the command line, go version prints its own
version information.

If a directory is named, go version walks that directory, recursively,
looking for recognized Go binaries and reporting their versions.
By default, go version does not report unrecognized files found
during a directory scan. The -v flag causes it to report unrecognized files.

The -m flag causes go version to print each executable's embedded
module version information, when available. In the output, the module
information consists of multiple lines following the version line, each
indented by a leading tab character.

See also: go doc runtime/debug.BuildInfo.

可见这个命令主要是用于输出 Go 可执行文件的编译信息的,只是如果没有提供可执行文件,则输出当前安装的 Go 版本信息。

我们通过一个具体例子来看看 -v、-m 的作用。

01 初始化例子

创建一个 go module 和如下目录结构:

$ go mod init github.com/polaris1119/gopher

$ tree .
.
├── cmd
│   ├── bar
│   │   └── main.go
│   └── foo
│       └── main.go
└── go.mod

其中 main.go 就是一个简单的 Hello World。执行 go install 安装。

$ export GOBIN=~/gopher/bin
$ go install github.com/polaris1119/gopher/cmd/...
  • export GOBIN 是为了将编译的结果放在当前目录的 bin 目录下,而不是默认的 $GOPATH/bin

成功后,执行 go version bin:

$ go version bin
bin/bar: go1.16.2
bin/foo: go1.16.2

而我本地的版本是 1.16.2。可见 go version [file …] 后的 file 可以是目录,这时会递归输出里面的文件的 Go 版本信息。

02 -v 选项

我们在 bin 目录下增加一个文本文件 api.txt 和一个可执行文件(php)。

$ tree bin
bin
├── api.txt
├── bar
├── foo
└── php

再次运行 go version:

$ go version bin
bin/bar: go1.16.2
bin/foo: go1.16.2

结果一样。试试加上 -v 参数。

$ go version -v bin
bin/api.txt: not executable file
bin/bar: go1.16.2
bin/foo: go1.16.2
bin/php: go version not found

可见 -v 参数能够输出无法识别的文件。

03 -m 选项

加上 -m 选项执行:(只看单个二进制文件,也可以跟上面一样是目录)

$ go version -m bin/foo
bin/foo: go1.16.2
	path	github.com/polaris1119/gopher/cmd/foo
	mod	github.com/polaris1119/gopher	(devel)

显示出当前二进制包路径和 mod 信息:包名和 devel。devel 表示这个二进制是开发版本。比如我们安装了 dlv,可以看看它的 Go 版本信息:(以下是我本地之前安装的)

$ go version -m ~/go/bin/dlv
/Users/xuxinhua/go/bin/dlv: go1.16beta1
	path	github.com/go-delve/delve/cmd/dlv
	mod	github.com/go-delve/delve	v1.6.0	h1:NImdy7K9essqNU8sazLhbX/oCicpmlapmjgA3qL1LZM=
	。。。

它没有 devel,而是具体的版本号(这里是 v1.6.0),h1:NImdy7K9essqNU8sazLhbX/oCicpmlapmjgA3qL1LZM= 这一串和 go.sum 中是一样的,h1: 是固定的,后面一串是 hash,是 Go modules 将目标模块版本的 zip 文件解包后,针对所有包内文件依次进行 hash,然后再把它们的 hash 结果按照固定格式和算法生成总的 hash 值。

下面,我们修改一下 cmd/foo/main.go 文件:(如果没有引入依赖,可以 go mod tidy 引入下)

package main

import (
  "fmt"
  "github.com/polaris1119/foo"
)

func main() {
	fmt.Println(foo.Bar())
}

然后 go install 安装:

$ go install github.com/polaris1119/gopher/cmd/...

再次 -m 选项看看:

$ go version -m bin/foo
bin/foo: go1.16.2
	path	github.com/polaris1119/gopher/cmd/foo
	mod	github.com/polaris1119/gopher	(devel)
	dep	github.com/polaris1119/foo	v0.4.0	h1:fgXsULdtXQmElR8Qor10s29CQbeA1pjSa/Cj0kB2Aas=

dep 表明该二进制程序依赖了 github.com/polaris1119/foo 这个包,版本是 v0.4.0 ,h1 hash 是 fgXsULdtXQmElR8Qor10s29CQbeA1pjSa/Cj0kB2Aas=,这个信息和 go.mod 中是一致的。

04 总结

现在如果有一个 Go 语言实现的二进制程序,我们可以通过 go version 命令分析出它使用的 Go 版本信息,以及依赖包的信息。有些时候也许需要了解这些信息。