前言
Go 作为一种可用于创建高性能网络和并发系统的编程语言,它的生态应用变得越来越广泛,同时,这也激发了开发人员使用 Go 作为脚本语言的兴趣。虽然目前 Go 还未准备好作为脚本语言 “开箱即用” 的特性,用来替代 Python 和 Bash ,但是我们只需要一点点准备工作就可以达到想要的目标。
Google 公司的软件工程师 Eyal Posener 为 Go 用作脚本语言提供了更多的理由,例如,丰富的标准库和语言的简洁性使得维护工作变得更加容易。
为了让 Go 编写的脚本在 shell 脚本程序中表现良好,Codenation 的工程师使用了许多有用的 Go 软件包:
- github.com/fatih/color 是用于输出对应编码颜色的包。
- github.com/schollz/progressbar 是用于为执行时间过久的任务创建进度条的包。
- github.com/jimlawless/whereami 是用于捕获源代码的文件名、行号、函数等信息的包,这对于改正错误信息十分有用!
- github.com/spf13/cobra 是用于更轻松地创建带有输入选项和相关文档的复杂脚本的包。
Hello
package mainimport ("fmt""os"
)func main() {fmt.Println("Hello", os.Args[1])os.Exit(42)
}
执行
$ go run example.go world
Hello world
exit status 42
$ echo $?
1
cobra
基本使用
package main import ( "fmt" "github.com/spf13/cobra" "os"
) func main() { // 创建根命令 var rootCmd = &cobra.Command{ Use: "app", Short: "应用程序描述", Long: `应用程序的详细描述,例如,它是用来做什么的`, Run: func(cmd *cobra.Command, args []string) { fmt.Println("欢迎使用 Cobra 应用程序!") }, }// 创建一个子命令 var helloCmd = &cobra.Command{ Use: "hello", Short: "打印问候语", Long: `此命令将打印出问候语给你指定的名字`, Run: func(cmd *cobra.Command, args []string) { if len(args) > 0 { fmt.Printf("你好, %s!\n", args[0]) } else { fmt.Println("你好, 世界!") } }, } // 将子命令添加到根命令 rootCmd.AddCommand(helloCmd) // 执行根命令 if err := rootCmd.Execute(); err != nil { fmt.Println(err) os.Exit(1) }
}
执行
go run main.go # 输出: 欢迎使用 Cobra 应用程序!
go run main.go hello # 输出: 你好, 世界!
go run main.go hello John # 输出: 你好, John!
参数介绍
-
cmd := &cobra.Command{}
:创建一个新的命令对象。可以设置命令的名称、简短描述、详细描述、执行函数等。cmd.Use
:设置命令的名称和用法。cmd.Short
:设置命令的简短描述。cmd.Long
:设置命令的详细描述。cmd.Run
:设置命令的执行函数。
-
rootCmd.AddCommand(cmd1, cmd2,...)
:将多个子命令添加到根命令中。 -
cmd.Flags().StringP("paramName", "p", "defaultValue", "description")
:添加一个字符串类型的命令行参数,带有短名称(-p)和长名称(–paramName)。可以指定默认值和参数描述。 -
cmd.Flags().IntP("intParam", "i", 0, "description")
:添加一个整数类型的命令行参数。 -
cmd.Flags().BoolP("boolParam", "b", false, "description")
:添加一个布尔类型的命令行参数。 -
err := cmd.Execute()
:执行命令。如果命令执行过程中出现错误,会返回一个错误对象。
progressbar
安装
go get -u github.com/schollz/progressbar/v3
bar := progressbar.Default(100)
for i := 0; i < 100; i++ {bar.Add(1)time.Sleep(40 * time.Millisecond)
}
执行的大致效果
资料
- https://www.infoq.com/news/2020/04/go-scripting-language/
- https://gist.github.com/posener/73ffd326d88483df6b1cb66e8ed1e0bd
- https://go.dev/blog/survey2019-results
- https://github.com/spf13/cobra
- https://github.com/schollz/progressbar