- Learning Functional Programming in Go
- Lex Sheehan
- 59字
- 2025-02-27 05:14:36
Authorization decorator
Our first decorator is Authorization. We call the Header helper function that adds the Authorization header with the given token to each request at runtime:
func Authorization(token string) Decorator {
return Header("Authorization", token)
}
func Header(name, value string) Decorator {
return func(c Client) Client {
return ClientFunc(func(r *http.Request)(*http.Response, error) {
r.Header.Add(name, value)
return c.Do(r)
})
}
}