将命名参数传递给 Golang 工具并使用它们的最简洁方法是什么?

What is the most concise way of passing named arguments to a Golang tool and to use them?

目标: 将命名参数传递给 Golang 工具并将传递的参数用作变量


尝试

编译 following example:

package main

import (
  "fmt"
  "os"
)

func main() {
  argsWithProg := os.Args
  argsWithoutProg := os.Args[1:]
  arg := os.Args[3]

  fmt.Println(argsWithProg)
  fmt.Println(argsWithoutProg)
  fmt.Println(arg)
}

构建它并传递参数,例如:./args -a=1 -b=2 -c=3 -d=4 -e=5 -f=6,结果为:

[./args -a=1 -b=2 -c=3 -d=4 -e=5 -f=6]
[-a=1 -b=2 -c=3 -d=4 -e=5 -f=6]
-c=3

基于此答案,示例中添加了以下代码段:

s := strings.Split(arg, "=")
variable, value := s[0], s[1]
fmt.Println(variable, value)

构建并传递参数后,输出如下:

[./args -a=1 -b=2 -c=3 -d=4 -e=5 -f=6]
[-a=1 -b=2 -c=3 -d=4 -e=5 -f=6]
-c=3
-c 3

问题

虽然目的已经达到,但我想知道这是否是传递命名参数并在 Golang 中使用它们的最简洁的方式。

Package flag

import "flag" 

Package flag implements command-line flag parsing.

试试 flag 和其他类似的软件包。