-
Notifications
You must be signed in to change notification settings - Fork 1
/
doc.go
46 lines (46 loc) · 1.51 KB
/
doc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// args is a generic library for optional arguments. It is
// inspired by Dave Cheney's functional options idea
// (http://dave.cheney.net/2014/10/17/functional-options-for-friendly-apis).
// It can serve the purpose of Python "kwargs" for Go programs.
//
// Optional arguments are defined using New and its typed variants:
//
//
// These arguments are basically functions that return argument values
// of type args.V. To use these argument values the function receives
// a variadic list of args.V and then get the value of each argument
// from those values:
//
// var RoundTripper = args.New()
// var Timeout = args.NewDuration()
// func MyServer(args ...args.V) {
// rt := RoundTripper.Get(args)
// to := Timeout.Get(args)
// ...
// }
// MyServer()
// MyServer(Timeout(1 * time.Second))
// MyServer(Timeout(2 * time.Second), RoundTripper(MyTransport))
//
// To use typed arguments, instead of the generic args.V,
// you'll need to write a few lines of boiler-plates:
//
// var roundTripper = args.New()
// var timeout = args.NewDuration()
//
// type ServerOpt args.V
// func RoundTripper(r http.RoundTripper) ServerOpt {
// return ServerOpt(roundTripper(r))
// }
// func Timeout(d time.Duration) ServerOpt {
// return ServerOpt(d)
// }
// func MyServer(opts ...ServerOpt) {
// rt := roundTripper.Get(opts).(http.RoundTripper)
// to := timeout.Get(opts)
// ...
// }
//
// Note that, args is focused on easy-to-use APIs. It is not efficient
// and is wasteful if the function is frequently invoked.
package args