Slide 19
Slide 19 text
package cmd
import (
"fmt"
"github.com/spf13/cobra"
)
var helloCmd = &cobra.Command{
Use: "hello",
Short: "A brief description of your command",
…省略…
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(“hello called”)
},
}
func init() {
rootCmd.AddCommand(helloCmd)
}
19
1. サブコマンドの構造体の初期化と
変数への代⼊
4. パッケージの初期化
(main関数よりも先に実⾏される)
5. コマンド本体へサブコマンドを追加
hello.go
3. サブコマンドで提供する処理を実装する
2. 無名関数を構造体に追加