Golang flag.FlagSet.String() function example

package flag

String defines a string flag with specified name(1st parameter), default value(2nd parameter), and usage string(3rd parameter). The return value is the address of a string variable that stores the value of the flag.

Golang flag.FlagSet.String() function usage example

 package main

 import (
 "flag"
 "fmt"
 )

 var (
 flagSet *flag.FlagSet
 p string
 )

 func main() {

 flagSet = flag.NewFlagSet("example", flag.ContinueOnError)


 filename := flagSet.String("file", "textfile.txt", "filename to view")


 flagSet.StringVar(&p, "timeout", "5000", "time to wait for response")

 err := flagSet.Parse([]string{"file", "timeout"})

 if err != nil {
 fmt.Println(err)
 }


 fmt.Println(*filename)
 fmt.Println(p)
 }

Output :

textfile.txt

5000

Reference :

http://golang.org/pkg/flag/#FlagSet.String

Advertisement