Skip to content

Instantly share code, notes, and snippets.

@gnilchee
Last active April 5, 2017 04:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gnilchee/88ecc10563954f8ceecc602f2325bc08 to your computer and use it in GitHub Desktop.
Save gnilchee/88ecc10563954f8ceecc602f2325bc08 to your computer and use it in GitHub Desktop.
fun with command line arguments
package main
import (
"flag"
"fmt"
)
func main() {
var (
secureAddress = flag.Bool("secure", false, "Set https as protocol")
hostAddress = flag.String("host", "example.com", "Hostname, include port if non-standard. i.e. example.com:8080")
hostPath = flag.String("path", "/", "Path URI.")
whatProto string
)
flag.Parse()
if *secureAddress {
whatProto = "https"
} else {
whatProto = "http"
}
fmt.Println(whatProto + "://" + *hostAddress + *hostPath)
}
# ./cmd_args
http://example.com:80/
# ./cmd_args --help
Usage of ./cmd_args:
-host string
Hostname, include port if non-standard. i.e. example.com:8080 (default "example.com")
-path string
Path URI. (default "/")
-secure
Set https as protocol
# ./cmd_args -secure -host google.com -path /robots.txt
https://google.com/robots.txt
# ./cmd_args -secure -host google.com:8443 -path /robots.txt
https://google.com:8443/robots.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment