Skip to content

Instantly share code, notes, and snippets.

@soheilhy
Last active August 29, 2015 14:25
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 soheilhy/961da121ba8a395f6315 to your computer and use it in GitHub Desktop.
Save soheilhy/961da121ba8a395f6315 to your computer and use it in GitHub Desktop.
custom typed args
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