使用 Go 语言开发一个命令行工具,用于向 OSS 文件上传文件 命令行工具有很多框架,选择使用 Cobra
准备
安装 Cobra
1
go get -u github.com/spf13/cobra/cobra
初始化项目
创建项目
1 2 3 4
cd$GOPATH/src mkdir oss cd oss cobra init --pkg-name oss
此时,已经创建了一个 oss 的目录,里面已经包含一个 root 命令
1 2 3 4 5 6 7
. ├── LICENSE ├── cmd │ └── root.go └── main.go
1 directory, 3 files
运行 go run main.go查看root 命令
1 2 3 4 5 6 7
go run main.go A longer description that spans multiple lines and likely contains examples and usage of using your application. For example:
Cobra is a CLI library for Go that empowers applications. This application is a tool to generate the needed files to quickly create a Cobra application.
添加 mod
添加mod 是为了更方便的管理依赖,module 设置 GitHub 是为了安装方便
1 2
export GO111MODULE=on go mod init github.com/helloworlde/oss
同时需要改下 mian.go中的包名,由oss/cmd改为 github.com/helloworlde/oss/cmd,如果是在GoLand 这样的 IDE 中,还需要启用Enable Go Modules (vgo) integration