How to get a list of values into a flag in Golang?

You can define your own flag.Value and use flag.Var() for binding it.

The example is here.

Then you can pass multiple flags like following:

go run your_file.go --list1 value1 --list1 value2

UPD: including code snippet right there just in case.

package main

import "flag"

type arrayFlags []string

func (i *arrayFlags) String() string {
    return "my string representation"
}

func (i *arrayFlags) Set(value string) error {
    *i = append(*i, value)
    return nil
}

var myFlags arrayFlags

func main() {
    flag.Var(&myFlags, "list1", "Some description for this param.")
    flag.Parse()
}

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)