gpt4 book ai didi

go - docker 命令行列表参数

转载 作者:数据小太阳 更新时间:2023-10-29 03:28:42 25 4
gpt4 key购买 nike

当我启动 docker 守护程序时,我正在修改 dns 服务器,以便容器具有经过修改的/etc/resolv.conf。查看我看到的用法消息:

$ docker --help
Usage: docker [OPTIONS] COMMAND [arg...]

A self-sufficient runtime for linux containers.

Options:
--api-enable-cors=false Enable CORS headers in the remote API
-b, --bridge="" Attach containers to a prexisting network bridge
use 'none' to disable container networking
--bip="" Use this CIDR notation address for the network bridge's IP, not compatible with -b
-D, --debug=false Enable debug mode
-d, --daemon=false Enable daemon mode
--dns=[] Force Docker to use specific DNS servers
--dns-search=[] Force Docker to use specific DNS search domains
-e, --exec-driver="native" Force the Docker runtime to use a specific exec driver

... etc ...

--dns 是我想要传递的,它显示了一个带有 [] 的“列表”,经过多次试验和错误,我终于让它工作了:

--dns 127.0.0.1 --dns 8.8.8.8

哪些存款:

nameserver 127.0.0.1
nameserver 8.8.8.8

进入/etc/resolv.conf 文件。

这是向 docker(可能还有任何 go)程序提供列表的正确方法吗?

最佳答案

这是一种向 Go 中的程序传递多个参数的方法,但肯定不是唯一的方法。这是通过定义一个实现 Value 的类型来实现的。界面。 flag包裹在 flag.Parse()遍历与名称匹配的参数列表到已注册的 Value并在 Value 上调用 Set(string) 函数.您可以使用它来将给定名称的每个值附加到 slice 。

type numList []int

func (l *numList) String() string {
return "[]"
}

func (l *numList) Set(value string) error {
number, err := strconv.Atoi(value)

if err != nil {
return fmt.Errorf("Unable to parse number from value \"%s\"", value)
}

*l = append(*l, number)
return nil
}

这个新类型可以注册为标志变量。在以下示例中,应用程序采用 n 个 num 命令行参数,这些参数被转换为整数并添加到列表中。

var numbers numList

func main() {
flag.Var(&numbers, "num", "A number to add to the summation"
flag.Parse()

sum := 0
for _, num := range numbers {
sum += num
}

fmt.Printf("The sum of your flag arguments is %d.\n", sum)
}

这可以通过字符串标志轻松完成,并让用户传递分隔列表。 Go 语言中没有既定的约定,每个应用程序都可以提供最适合的任何实现。

关于go - docker 命令行列表参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29039031/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com