Last active
August 29, 2015 14:25
-
-
Save soheilhy/961da121ba8a395f6315 to your computer and use it in GitHub Desktop.
custom typed args
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var port = args.NewInt(args.Flag("example.typed.port", 1234, "the port")) | |
var roundTripper = args.New(args.Default(http.DefaultTransport)) | |
var timeout = args.NewDuration() | |
type ServerOpt args.V | |
func Port(p int) ServerOpt { return ServerOpt(port(p)) } | |
func RoundTripper(r http.RoundTrippper) ServerOpt { return ServerOpt(roundTripper(r)) } | |
func Timeout(d time.Duration) ServerOpt { return ServerOpt(timeout(d)) } | |
func MyServer(opts ...ServerOpt) { | |
port := port.Get(opts) | |
fmt.Printf("listening on port %v\n", port) | |
rt := roundTripper.Get(opts).(http.RoundTripper) | |
if rt == http.DefaultTransport { | |
fmt.Println("using the default transport") | |
} else { | |
fmt.Println("using a user-provided round-tripper") | |
} | |
to := timeout.Get(opts) | |
fmt.Printf("using a timeout of %v\n", to) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment