Help us shape the future of Go! Take the 2026 Go Developer Survey

cmd

package
v0.8.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 28, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package cmd 用 cobra 组织命令行。

Go 小白知识点:

  • cobra 是 Go 生态最流行的命令行库(kubectl、hugo、gh 都用它)。
  • cobra.Command 代表一条命令;Use 是命令名称,Short 是单行简介,Long 是 --help 全文。
  • rootCmd 是「根命令」,所有子命令(如 gen)都挂在它下面,形成命令树。
  • init() 是 Go 的包初始化函数,在 main 运行前自动调用,不可手动调用。 每个 .go 文件可以有自己的 init();同一包内多个 init() 按文件名字母序执行。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Execute

func Execute() error

Execute 由 main 调用,启动整个命令树的解析与执行。 cobra.Command.Execute() 内部会:

  1. 解析 os.Args(命令行参数)
  2. 找到对应子命令
  3. 绑定 flags
  4. 调用 RunE / Run 回调

func SetVersion added in v0.8.0

func SetVersion(v string)

SetVersion 由 main 在启动时调用,把构建期注入的版本号交给根命令。

空串会被忽略——ldflags 未注入时 main.version 可能是空串,若直接赋值会让 --version 输出 "base-code version " 这种半截结果,不如保留缺省的 dev 有意义。

Go 小白知识点:版本号走「构建期注入」而非写死在源码里,是 Go 项目的通行做法—— 编译时用 -ldflags "-X 包路径.变量名=值" 改写某个包级 string 变量的初值, 这样同一份源码能编出带不同版本号的产物,不必为发版改一行代码再提交。

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL