These are all bad examples as you would use it on the I/O and other things which would change through the stack and as a result fairly contrived.
Last active
January 26, 2022 08:19
-
-
Save arran4/a01ecd3e71510102b3369df6efe837c4 to your computer and use it in GitHub Desktop.
This file contains 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" | |
) | |
// Library | |
context ( | |
logprefix string = "" | |
) | |
func SetPrefix(s string) { | |
logprefix = s | |
} | |
func Log(s string) { | |
fmt.Printf("%s: %s", logprefix, s) | |
} | |
// Client | |
func c(){} | |
func b() { | |
SetPrefix("[B]") | |
Log("2") | |
c() | |
Log("2.1") | |
} | |
func a() { | |
SetPrefix("[A]") | |
Log("1") | |
b() | |
Log("1.1") | |
} | |
func main() { | |
a() | |
} |
This file contains 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" | |
) | |
// Library | |
var ( | |
logprefix string = "" | |
) | |
func SetPrefix(s string) { | |
logprefix = s | |
} | |
func GetPrefix() string { | |
return logprefix | |
} | |
func Log(s string) { | |
fmt.Printf("%s: %s", logprefix, s) | |
} | |
// Client | |
func c(){} | |
func b() { | |
p := GetPrefix() | |
SetPrefix("[B]") | |
defer func() { | |
SetPrefix(p) | |
} | |
Log("2") | |
c() | |
Log("2.1") | |
} | |
func a() { | |
p := GetPrefix() | |
SetPrefix("[A]") | |
defer func() { | |
SetPrefix(p) | |
} | |
Log("1") | |
b() | |
Log("1.1") | |
} | |
func main() { | |
a() | |
} |
This file contains 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" | |
) | |
// Library | |
var ( | |
logprefix string = "" | |
) | |
type PreviousValue string | |
func (p PreviousValue) Close() error { | |
logprefix = string(p) | |
return nil | |
} | |
func SetPrefix(s string) PreviousValue { | |
pv, logprefix = PreviousValue(logprefix), s | |
return pv | |
} | |
func Log(s string) { | |
fmt.Printf("%s: %s", logprefix, s) | |
} | |
// Client | |
func c(){} | |
func b() { | |
p := SetPrefix("[B]") | |
defer p.Close() | |
defer func() { | |
SetPrefix(p) | |
} | |
Log("2") | |
c() | |
Log("2.1") | |
} | |
func a() { | |
p := SetPrefix("[A]") | |
defer p.Close() | |
Log("1") | |
b() | |
Log("1.1") | |
} | |
func main() { | |
a() | |
} |
This file contains 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" | |
) | |
// Library | |
func Log(s string) { | |
fmt.Printf("%s", s) | |
} | |
// Client | |
func c(){} | |
func b(prefix string) { | |
prefix = prefix + "[A]" | |
Log(prefix + ": 2") | |
c() | |
Log(prefix + ": 2.1") | |
} | |
func a(prefix string) { | |
np := prefix + "[B]" | |
Log(np + ": 1") | |
b(prefix) | |
Log(np + ": 1.1") | |
} | |
func main() { | |
a("") | |
} |
This file contains 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" | |
) | |
// Library | |
type Logger struct { | |
Prefix string | |
} | |
func (l *Logger) Log(s string) { | |
fmt.Printf("%s: %s", l.Prefix, s) | |
} | |
// Client | |
func c(){} | |
func b(l *Logger) { | |
l.Prefix = "[B]" | |
l.Log(prefix + ": 2") | |
c() | |
l.Log(prefix + ": 2.1") | |
} | |
func a(l *Logger) { | |
l.Prefix = "[A]" | |
Log(np + ": 1") | |
b(l) | |
l.Prefix = "[A]" | |
Log(np + ": 1.1") | |
} | |
func main() { | |
l := &Logger{ | |
Prefix: "", | |
} | |
a(l) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment