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 :
Advertisement
Something interesting
Tutorials
+16.4k CodeIgniter/PHP : Create directory if does not exist example
+6k Golang : Function as an argument type example
+9.9k Golang : Function wrapper that takes arguments and return result example
+11.9k Golang : Convert decimal number(integer) to IPv4 address
+5.3k Golang : Pad file extension automagically
+6.2k Golang : Process non-XML/JSON formatted ASCII text file example
+9k Golang : Capture text return from exec function example
+15.2k Golang : Save(pipe) HTTP response into a file
+8.3k Useful methods to access blocked websites
+21.2k Golang : Get password from console input without echo or masked
+12.3k Golang : How to check if a string starts or ends with certain characters or words?