Skip to content

Instantly share code, notes, and snippets.

@msmith
Last active July 3, 2019 21:07
Show Gist options
  • Save msmith/f1d6817a6872dc57f4a1c1da685f9160 to your computer and use it in GitHub Desktop.
Save msmith/f1d6817a6872dc57f4a1c1da685f9160 to your computer and use it in GitHub Desktop.
Avoiding global state
package main
import "fmt"
var Enabled bool
func LogEvent(msg string) {
if Enabled {
fmt.Println(msg)
}
}
func doThing() {
LogEvent("doing a thing")
}
func main() {
Enabled = true
doThing()
}
package main
import "fmt"
type Logger struct {
Enabled bool
}
func (l *Logger) LogEvent(msg string) {
if l.Enabled {
fmt.Println(msg)
}
}
func doThing(l *Logger) {
l.LogEvent("doing a thing")
}
func main() {
l := Logger{Enabled: true}
doThing(l)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment