Last active
July 3, 2019 21:07
-
-
Save msmith/f1d6817a6872dc57f4a1c1da685f9160 to your computer and use it in GitHub Desktop.
Avoiding global state
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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