golang 常用库


原文链接: golang 常用库

##go 语言包
time

命令行
https://github.com/urfave/cli.git

主函数 ./main.go

package main

import (
	"os"

	"github.com/rinetd/transfer/cmd"
)

func main() {
	if err := cmd.Run(); err != nil {
		os.Exit(0)
	}
}

子目录 ./cmd/root.go

package main

import (
  "fmt"
  "os"

  "github.com/urfave/cli"
)

func Run() error {
	app := cli.NewApp()
	app.Name = `Transfer`
	app.Email = "https://github.com/rinetd"
	app.Usage = `Translate YAML, JSON, TOML, HCL, XML, properties ...
   transfer [-f] [-s input.yaml] [-o output.json] /path/to/input.yaml [/path/to/output.json]`
  app.UsageText = `详细说明`
	app.Version = version.Version
  app.Action = func(c *cli.Context) error {
    fmt.Println(c.NArg())
    fmt.Println(c.Args().First())
    fmt.Println(c.Args().Get(1))
    //  go run main.go 123 -a 1 -b 34 1
    // 共 6 个参数 
    // 第一个参数是 123
    // 第二个参数是  1
    switch c.NArg() {
      // 根据参数的不同,决定程序执行的方式
      case 0:
        c.ShowAppHelp(c) //显示帮助页
      case 1:
    }

    fmt.Println("boom! I say!")
    return nil
  }

  // 命令行标志
  app.Flags = []cli.Flag{
		cli.StringFlag{
			Name: "s, input",
			// Value: "yaml",
			Usage: "input Type: json, yaml, toml, hcl, xml",
		},
		cli.StringFlag{
			Name: "o, output",
			// Value: "json",
			Usage: "output file : json, yaml, toml, hcl, xml",
		},
		cli.BoolFlag{
			Name:  "f,force",
			Usage: "force cover output file",
		},
  }
  // 入口函数
  return app.Run(os.Args)
}
`