如何解析 Golang 中的参数列表(多个重复项和逗号分隔)?

How to parse a list of parameters in Golang (multiple duplicate and comma separated)?

我需要在 GO 中解析不同的参数(多个重复项并以逗号分隔)。对于这个例子我该如何做:

go run ./test.go -param "one, two" -param "tree" -param "four"

This example 很好,但不适用于上述示例:

[one, two tree four]

即它适用于多个重复参数,但不适用于逗号分隔。

我如何改进上述脚本以解析多个参数,包括以逗号分隔的参数以获得结果中的这个(无逗号):

[one two tree four]

?

修改 Set 方法,使其在逗号处拆分其参数并将结果附加到接收方。

func (i *arrayFlags) Set(value string) error {
    s := strings.Split(value, ",")
    for i := range s {
        s[i] = strings.TrimSpace(s[i])
    }
    *i = append(*i, s...)
    return nil
}