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
+21.2k Golang : Convert(cast) string to rune and back to string example
+20.7k Android Studio : AlertDialog and EditText to get user string input example
+8.1k Golang : Tell color name with OpenCV example
+9.9k Golang : Sort and reverse sort a slice of integers
+13k Golang : Get terminal width and height example
+5.9k AWS S3 : Prevent Hotlinking policy
+17.7k Golang : Read data from config file and assign to variables
+7.9k Golang : Grayscale Image
+18k Golang : Get all upper case or lower case characters from string example
+14.5k Golang : Rename directory
+5.8k Unix/Linux : How to test user agents blocked successfully ?