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
+13.9k Golang : Human readable time elapsed format such as 5 days ago
+11.9k Golang : Setup API server or gateway with Caddy and http.ListenAndServe() function example
+7.6k Golang : Set horizontal, vertical scroll bars policies and disable interaction on Qt image
+19.1k Mac OSX : Homebrew and Golang
+15.2k Golang : Save(pipe) HTTP response into a file
+22.2k Golang : Print leading(padding) zero or spaces in fmt.Printf?
+5k Golang : Calculate a pip value and distance to target profit example
+5.3k Javascript : Shuffle or randomize array example
+14k Golang : Fix cannot use buffer (type bytes.Buffer) as type io.Writer(Write method has pointer receiver) error
+9.7k Golang : List available AWS regions
+6.7k Golang : Humanize and Titleize functions
+3.6k Java : Get FX sentiment from website example