Standard library / flag
flag.Value
Custom flag interface; String represents the value and Set parses one occurrence. A zero-valued or nil receiver may be passed to String by help formatting. Expected stdout: 'a,b\n'.
Formal Syntax & Structure
go syntaxgo
type Value interface { String() string Set(string) error}Working Example
Example Snippetgo
package main
import ( "flag" "fmt" "strings")
func check(e error) { if e != nil { panic(e) }}
type tagList []string
func (t *tagList) String() string { if t == nil { return "" } return strings.Join(*t, ",")}func (t *tagList) Set(s string) error { *t = append(*t, s); return nil }
func main() { f := flag.NewFlagSet("demo", flag.ContinueOnError) var tags tagList var v flag.Value = &tags f.Var(v, "tag", "tag") check(f.Parse([]string{"-tag=a", "-tag=b"})) fmt.Println(v.String())}