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
+18.7k Golang : Iterating Elements Over A List
+5.9k Golang : Use NLP to get sentences for each paragraph example
+7.4k Golang : Convert source code to assembly language
+7k Golang : Levenshtein distance example
+9.4k Golang : Find the length of big.Int variable example
+7.5k Golang : How to handle file size larger than available memory panic issue
+7.8k Golang : Reverse a string with unicode
+16.3k Golang : Loop each day of the current month example
+5.8k Javascript : How to replace HTML inside <div>?
+12.7k Golang : Sort and reverse sort a slice of bytes
+5.7k Golang : Frobnicate or tweaking a string example
+5.7k Unix/Linux/MacOSx : Get local IP address