Golang flag.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.String() function usage example

 package main

 import (
 "flag"
 "fmt"
 )

 func main() {

 verbose := flag.String("verbose", "on | off", "Turn verbose mode on or off.")
 flag.Parse()

 fmt.Println(flag.Lookup("verbose")) // print the Flag struct

 fmt.Println(*verbose)

 }

Output :

&{verbose Turn verbose mode on or off. on | off on | off}

on | off

Reference :

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

Advertisement