Skip to content

Instantly share code, notes, and snippets.

@andreagrandi
Created August 21, 2014 10:27
Show Gist options
  • Save andreagrandi/c7ad4307ee61812084ac to your computer and use it in GitHub Desktop.
Save andreagrandi/c7ad4307ee61812084ac to your computer and use it in GitHub Desktop.
Parse command line flags in Go
package main
import (
"flag"
"fmt"
)
var hostName = flag.String("host", "localhost", "Hostname or IP you want to run this service on")
var portNumber = flag.Int("port", 8080, "Port you want this service to listen on (default 8080)")
func main() {
flag.Parse()
fmt.Println(*hostName)
fmt.Println(*portNumber)
}
@andreagrandi
Copy link
Author

Example: go run cmd_flag_parser.go -host example.com -port 3000

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment