Skip to content

Instantly share code, notes, and snippets.

@ian-kent
Created February 6, 2015 21:06
Show Gist options
  • Save ian-kent/c419db3ef74426b69067 to your computer and use it in GitHub Desktop.
Save ian-kent/c419db3ef74426b69067 to your computer and use it in GitHub Desktop.
My new favourite golang config pattern
package config
import "flag"
var BindAddress string
var SMTPHost string
var SMTPPort int
func Configure() {
flag.StringVar(&BindAddress, "bind", ":9650", "Bind address")
flag.StringVar(&SMTPHost, "smtp-host", "localhost", "SMTP server host")
flag.IntVar(&SMTPPort, "smtp-port", 587, "SMTP server port")
flag.Parse()
}
var _ = func() { Configure() }
@markcol
Copy link

markcol commented Feb 7, 2015

Why not just rename Configure() to init() which eliminates the need for the 'var _' declaration?

Using an unbound variable declaration to invoke a function for it's side-effects is esoteric, at best.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment