<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          發(fā)現(xiàn) go version 的一個(gè)另類(lèi)用法:你肯定想不到

          共 4639字,需瀏覽 10分鐘

           ·

          2021-03-19 18:21

          閱讀本文大概需要 6 分鐘。

          大家好,我是站長(zhǎng) polarisxu。

          對(duì)于 go version,大家應(yīng)該不陌生。在很多入門(mén)教程,安裝 Go 后,一般會(huì)建議執(zhí)行 go version 看看是否安裝成功;亦或遇到問(wèn)題,別人會(huì)問(wèn)你 Go 哪個(gè)版本,你也會(huì)通過(guò) go version 命令查看。所以,go version 的一個(gè)作用是查看本地使用的 Go 版本。

          但實(shí)際上,go version 還有其他用途,甚至可以說(shuō),輸出本地 Go 版本號(hào)只是它功能的一個(gè)特例。先 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.

          可見(jiàn)這個(gè)命令主要是用于輸出 Go 可執(zhí)行文件的編譯信息的,只是如果沒(méi)有提供可執(zhí)行文件,則輸出當(dāng)前安裝的 Go 版本信息。

          我們通過(guò)一個(gè)具體例子來(lái)看看 -v、-m 的作用。

          01 初始化例子

          創(chuàng)建一個(gè) go module 和如下目錄結(jié)構(gòu):

          $ go mod init github.com/polaris1119/gopher

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

          其中 main.go 就是一個(gè)簡(jiǎn)單的 Hello World。執(zhí)行 go install 安裝。

          export GOBIN=~/gopher/bin
          $ go install github.com/polaris1119/gopher/cmd/...
          • export GOBIN 是為了將編譯的結(jié)果放在當(dāng)前目錄的 bin 目錄下,而不是默認(rèn)的 $GOPATH/bin

          成功后,執(zhí)行 go version bin:

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

          而我本地的版本是 1.16.2。可見(jiàn) go version [file …] 后的 file 可以是目錄,這時(shí)會(huì)遞歸輸出里面的文件的 Go 版本信息。

          02 -v 選項(xiàng)

          我們?cè)?bin 目錄下增加一個(gè)文本文件 api.txt 和一個(gè)可執(zhí)行文件(php)。

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

          再次運(yùn)行 go version:

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

          結(jié)果一樣。試試加上 -v 參數(shù)。

          $ 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

          可見(jiàn) -v 參數(shù)能夠輸出無(wú)法識(shí)別的文件。

          03 -m 選項(xiàng)

          加上 -m 選項(xiàng)執(zhí)行:(只看單個(gè)二進(jìn)制文件,也可以跟上面一樣是目錄)

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

          顯示出當(dāng)前二進(jìn)制包路徑和 mod 信息:包名和 devel。devel 表示這個(gè)二進(jìn)制是開(kāi)發(fā)版本。比如我們安裝了 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=
           。。。

          它沒(méi)有 devel,而是具體的版本號(hào)(這里是 v1.6.0),h1:NImdy7K9essqNU8sazLhbX/oCicpmlapmjgA3qL1LZM= 這一串和 go.sum 中是一樣的,h1: 是固定的,后面一串是 hash,是 Go modules 將目標(biāo)模塊版本的 zip 文件解包后,針對(duì)所有包內(nèi)文件依次進(jìn)行 hash,然后再把它們的 hash 結(jié)果按照固定格式和算法生成總的 hash 值。

          下面,我們修改一下 cmd/foo/main.go 文件:(如果沒(méi)有引入依賴(lài),可以 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 選項(xiàng)看看:

          $ 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 表明該二進(jìn)制程序依賴(lài)了 github.com/polaris1119/foo 這個(gè)包,版本是 v0.4.0 ,h1 hash 是 fgXsULdtXQmElR8Qor10s29CQbeA1pjSa/Cj0kB2Aas=,這個(gè)信息和 go.mod 中是一致的。

          04 總結(jié)

          現(xiàn)在如果有一個(gè) Go 語(yǔ)言實(shí)現(xiàn)的二進(jìn)制程序,我們可以通過(guò) go version 命令分析出它使用的 Go 版本信息,以及依賴(lài)包的信息。有些時(shí)候也許需要了解這些信息。

          如果覺(jué)得有用,給個(gè)贊唄!




          往期推薦


          歡迎關(guān)注我

          都看到這里了,隨手點(diǎn)個(gè)贊支持下唄!


          瀏覽 58
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  99久久婷婷国产精品2020 | 在线免费观看一级a片 | 啊啊草,在厕所操视频 | 五月丁香六月婷婷网 | 亚洲丝袜足交 |