import ( "fmt" "github.com/spf13/pflag" ) func main() { verbose := pflag.BoolP("verbose", "v", false, "Enable verbose logging") port := pflag.IntP("port", "p", 8080, "The port to listen on") pflag.Parse() fmt.Printf("Verbose logging enabled: %v\n", *verbose) fmt.Printf("Listening on port: %d\n", *port) }
import ( "fmt" "github.com/spf13/pflag" ) func main() { configPath := pflag.String("config", "", "Path to config file") pflag.Parse() if *configPath == "" { fmt.Println("No config file specified") } else { fmt.Printf("Using config file: %s\n", *configPath) } }In this example, we create a single string flag for specifying the path to a config file. If the flag is not specified, we print a message indicating that no config file was provided. Otherwise, we print out the path to the config file. Overall, the pflag package provides a useful enhancement to the standard flag package in Go, allowing for more complex command-line argument handling with aliases and defaults.