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
+36k Golang : Get file last modified date and time
+9.8k Golang : Resumable upload to Google Drive(RESTful) example
+13.8k Golang : unknown escape sequence error
+18.4k Golang : Read binary file into memory
+38.1k Golang : Read a text file and replace certain words
+18k Golang : Get all upper case or lower case characters from string example
+9.6k Golang : Read file with ioutil
+18.4k Golang : How to get hour, minute, second from time?
+10.6k Golang : How to unmarshal JSON inner/nested value and assign to specific struct?
+15k Golang : Search folders for file recursively with wildcard support
+5.9k Golang : Extract unicode string from another unicode string example
+10.8k Android Studio : Checkbox for user to select options example