通过此文可初步掌握cobra,viper,docker,对称秘钥非对称秘钥的的使用,实现第一个简单的golang项目。
第一步 项目初始化,配置cobra
要在Go中使用go mod
从零开始创建一个新项目,你可以按照以下步骤操作:
-
创建项目目录: 你已经创建了名为
data-provider
的目录,接下来进入该目录。mkdir data-provider cd data-provider/
-
初始化新的Go模块: 在项目目录中,运行
go mod init
命令来初始化一个新的模块。你需要为你的模块指定一个唯一的名称,这通常是你的域名的反向表示,以确保唯一性。go mod init github.com/yourusername/data-provider
将
github.com/yourusername/data-provider
替换为你的实际用户名和项目路径
3.若使用wsl把代理ip变为主机ipconfig显示ip
4.go get 和go install的区别
root@LAPTOP-LU4HDAFV:~/data-provider# go get -u github.com/spf13/cobra/cobra
go: downloading github.com/spf13/cobra v1.8.1
go: module github.com/spf13/cobra@upgrade found (v1.8.1), but does not contain package github.com/spf13/cobra/cobra
go install github.com/spf13/cobra-cli@latest
验证安装
cobra创建main.go函数
root@LAPTOP-LU4HDAFV:~/data-provider# cobra-cli init ./
Your Cobra application is ready at
/root/data-provider/./
root@LAPTOP-LU4HDAFV:~/data-provider#
生成main.go和root.go
测试
修改main函数 和root.go使得出现错误时打印错误后退出
第二步 生成server.go 和version.go
-
添加
server
命令:cobra-cli add server
这将创建一个新的
server.go
文件,并在root.go
中注册这个命令。 -
添加
version
命令:cobra-cli add version
这将创建一个
version.go
文件,并在root.go
中注册这个命令。 -
此时代码结构
data-provider/
├── cmd/
│ ├── root.go
│ ├── server.go
│ └── version.go
├── go.mod
└── go.sum
4.创建使用测试
root@LAPTOP-LU4HDAFV:~/data-provider# go build -o data-provider
root@LAPTOP-LU4HDAFV:~/data-provider# ./data-provider
Usage:
[command]
Available Commands:
completion Generate the autocompletion script for the specified shell
help Help about any command
server A brief description of your command
version A brief description of your command
Flags:
-h, --help help for this command
Use " [command] --help" for more information about a command.
root@LAPTOP-LU4HDAFV:~/data-provider# ./data-provider server
server called
root@LAPTOP-LU4HDAFV:~/data-provider# ./data-provider version
version called
root@LAPTOP-LU4HDAFV:~/data-provider#
5.自动生成的server.go
/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"fmt"
"github.com/spf13/cobra"
)
// serverCmd represents the server command
var serverCmd = &cobra.Command{
Use: "server",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. 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.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("server called")
},
}
func init() {
rootCmd.AddCommand(serverCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// serverCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// serverCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
6.自动生成的version.go
/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"fmt"
"github.com/spf13/cobra"
)
// versionCmd represents the version command
var versionCmd = &cobra.Command{
Use: "version",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. 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.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("version called")
},
}
func init() {
rootCmd.AddCommand(versionCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// versionCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// versionCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}