Skip to content

Instantly share code, notes, and snippets.

@poga
Created January 14, 2015 07:58
Show Gist options
  • Save poga/bf0534486199c8e778cb to your computer and use it in GitHub Desktop.
Save poga/bf0534486199c8e778cb to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
)
type Service struct {
ErrorLevel int
AWSKey string
AWSSecret string
AfterHook func()
}
func (s *Service) Config(opts ...option) {
for _, opt := range opts {
opt(s)
}
}
type option func(*Service)
const (
DEBUG = iota
INFO
WARN
FATAL
PANIC
)
func LogLevel(level int) option {
return func(s *Service) {
s.ErrorLevel = level
}
}
func AfterComplete(f func()) option {
return func(s *Service) {
s.AfterHook = f
}
}
type S3 struct {
Key string
Secret string
}
func Backend(opt S3) option {
return func(s *Service) {
s.AWSKey = opt.Key
s.AWSSecret = opt.Secret
}
}
func ConfigService(s *Service) {
s.Config(
LogLevel(WARN),
Backend(S3{
Key: "API_KEY",
Secret: "SUPER SECRET",
}),
AfterComplete(func() {
fmt.Println("service ready")
}),
)
}
func main() {
s := Service{}
ConfigService(&s)
fmt.Printf("%v", s)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment