Comments (6)
Hi,
Don't you think that in general Singletons are a bad practice because they behave like global state and because that code that uses singletons are difficult to test? e.g https://storage.googleapis.com/gtb/TotT-2008-05-15.pdf
Related with this, what do you think about a dependency injection tutorial instead?
Thanks and keep up the good work!
from go-in-5-minutes.
@gonzaloserrano first of all, so awesome that you cited a 'testing on the toilet' paper!
anyway, I agree that in general they're a bad practice. generally I've seen this pattern applied in cases where you can also create an identical instance of the singleton if needed (e.g. for testing purposes). here's what it'd look like in a very simple logging package (this is similar to what's in the stdlib's log
package):
package log
type Logger struct {}
func NewLogger() *Logger {
return &Logger{}
}
func (l *Logger) Print(s string) {
fmt.Println(s)
}
var singleton = NewLogger()
func Print(s string) {
singleton.Print(s)
}
so, basically, the optional singleton pattern is for convenience, and you'd test against a *Logger
. thoughts?
from go-in-5-minutes.
Hi again,
In other languages (e.g PHP) singletons are global (e.g in PHP is implemented a static class). In the this case (and the go log
package with the std
var) this singleton is not exported so it's just accessible to the package, so i'd say it's not that harmful (although if that variable instantiation would provoke a side-effect then testing could be trickier).
Anyway i'm not a testing expert :-)
from go-in-5-minutes.
you nailed it, and I think alluded to a few rules of this pattern:
- the singleton variable must be private to the package
- the singleton variable instantiation should not have side effects
- the package should use an exported function or exported type to create the singleton variable
and I'd like to propose one more:
- convenience functions that operate on the singleton variable must simply operate on the type's functions
thoughts?
from go-in-5-minutes.
Sounds like a pretty good abstraction to me!
from go-in-5-minutes.
awesome, glad you agree. I'm going to be covering this pattern soon
from go-in-5-minutes.
Related Issues (20)
- Add vendor directories for all episodes
- Working with Distributed messaging service like NSQ in Go HOT 3
- Implement Oauth2 in golang for Google and Facebook using google oauth2 library
- How do I can change a variable with go command line? HOT 2
- How to upload a file to database in buffalo HOT 1
- Better Analytics
- Move to a different CI provider
- Rewrite the site?
- Split up top level .gitignore into individual directories
- Buffalo series: How to generate new models
- Buffalo series: how to write migrations
- Buffalo series: how to do basic queries with pop
- Buffalo series: how to do advanced queries with pop
- Buffalo series: how to run your test database locally
- Kubernetes for Go Developers
- Pls suggest framework to build in go microservices
- Creating Apis with GraphQL
- The many uses of io.Reader / io.Writer
- master => main branch
- Screencast about building a static site generator
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from go-in-5-minutes.