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
+8.6k Golang : Add text to image and get OpenCV's X, Y co-ordinates example
+11.6k Golang : Display a text file line by line with line number example
+15.2k Golang : How to add color to string?
+16.8k Golang : Get own process identifier
+5.5k Clean up Visual Studio For Mac installation failed disk full problem
+4.8k PHP : Extract part of a string starting from the middle
+6.7k Golang : Skip or discard items of non-interest when iterating example
+16.3k Golang :Trim white spaces from a string
+15.6k Chrome : ERR_INSECURE_RESPONSE and allow Chrome browser to load insecure content
+12.1k Golang : md5 hash of a string
+12.9k Python : Convert IPv6 address to decimal and back to IPv6