Live data from Hacker News

Singleton Pattern in Go

marcio.io

31–40 of 76 posts

Re: Singleton Pattern in Go

#31

Coding Pro Tip: Instead of using the Singleton pattern, make one instance of your class at the start of your program and pass it around to its users. Singletons are really just as bad as global variables. Why? Because they are global variables.

>Singletons are really just as bad as global variables. Why? Because they are global variables.

And just as handy as global variables.

Re: Singleton Pattern in Go

#32
What's the point of a singleton when you can just have variables in an anonymous struct at root level, and functions defined at root as well?

  var server struct {
  	host string
  	port string
  }
  
  func serverStart() {
  	server.host = "..."
  	server.port = "..."
  }

Re: Singleton Pattern in Go

#33
post #31

Coding Pro Tip: Instead of using the Singleton pattern, make one instance of your class at the start of your program and pass it around to its users. Singletons are really just as bad as global variables. Why? Because they are global variables.

> Singletons are really just as bad as global variables. Why? Because they are global variables. And just as handy as global variables.

> And just as handy as global variables.

which means not really handy in a non-thread safe context.

Re: Singleton Pattern in Go

#34

Coding Pro Tip: Instead of using the Singleton pattern, make one instance of your class at the start of your program and pass it around to its users. Singletons are really just as bad as global variables. Why? Because they are global variables.

Depending on what you are trying to implement - passing an extra variable everywhere is just creating noise (because potentially you have to have an extra parameter to EVERY function or method). I believe in KISS - everything should be as simple as possible but not simpler. Believe it or not there are valid uses for a Singleton which, many would agree, include a simple logging class [1].

One could argue that you could just create it in the section of code you want to log - but that just creates noise and you end up repeating code.

logger = Log() # run code to open the file to the last position logger.log("test")

vs

Log.instance().log("test")

Now imagine this was a multi-threaded application - singleton arguments are much different.

> Singletons are really just as bad as global variables. Why? Because they are global variables.

Everyone has their own opinions - but you can't make sweeping generalizations. I'm not saying a global variable is appropriate in every situation - but every language, and project, is different. There are even different dialects of C++ [2].

[1] - http://stackoverflow.com/questions/228164/on-design-patterns...

[2] - http://www.reddit.com/r/programming/comments/197dn1/introduc...

Re: Singleton Pattern in Go

#35

Earlier quoted context omitted.

Yea it's easy enough to use dependency injection to satisfy these supposedly "global" requirements. About the only global state I can tolerate is read-only constants, but even then you are much better off with DI just because of testing.

DI is an antipattern.

who told you that? you'd better not listen to that guy anymore.

Re: Singleton Pattern in Go

#36
post #6

I thought there was a lot of literature pretty convincingly arguing that singleton is in fact an anti-pattern.

Singletons are useful and wide spread concepts. What is an anti pattern is implementing them in a way that's not thread safe (which is made worse if your singleton is mutable). Letting DI frameworks handle your singletons is the best way to get the best of both worlds.

No no, singletons are pathological liars http://misko.hevery.com/2008/08/17/singletons-are-pathologic...

Atomic mutable global state is still mutable global state. DI containers mean you don't need a singleton but can just create a single instance which lets you scope it and does not mix lifetime duration and object properties.

Re: Singleton Pattern in Go

#37
post #36

Earlier quoted context omitted.

Singletons are useful and wide spread concepts. What is an anti pattern is implementing them in a way that's not thread safe (which is made worse if your singleton is mutable). Letting DI frameworks handle your singletons is the best way to get the best of both worlds.

No no, singletons are pathological liars http://misko.hevery.com/2008/08/17/singletons-are-pathologic... Atomic mutable global state is still mutable global state. DI containers mean you don't need a singleton but can just create a single instance which lets you scope it and does not mix lifetime duration and object properties.

> No no, singletons are pathological liars http://misko.hevery.com/2008/08/17/singletons-are-pathologic....

No, only badly implemented ones are.

Besides, this article is seven years old and it was written way before DI with Guice or Dagger became a thing. It's a very outdated perspective (and you will notice that AngularJS, which Misko contributes to, supports both DI and singletons).

Re: Singleton Pattern in Go

#38
post #27

Earlier quoted context omitted.

Yea it's easy enough to use dependency injection to satisfy these supposedly "global" requirements. About the only global state I can tolerate is read-only constants, but even then you are much better off with DI just because of testing.

How do handle configuration? Your program starts up, generates some settings, then those need to be shared across all threads to avoid incurring the cost of regenerating during every call to a function.

But in that case config values are to be treated as constants (read only). You don't have concurrency issues for code that doesn't change.

Re: Singleton Pattern in Go

#39
post #32

What's the point of a singleton when you can just have variables in an anonymous struct at root level, and functions defined at root as well? var server struct { host string port string } func serverStart() { server.host = "..." server.port = "..." }

When do you call "serverStart()"? If the answer is "eagerly during startup", there's no problem (other than startup time, of course). If the answer is "on demand", now you have a synchronization problem.

Re: Singleton Pattern in Go

#40
post #32

What's the point of a singleton when you can just have variables in an anonymous struct at root level, and functions defined at root as well? var server struct { host string port string } func serverStart() { server.host = "..." server.port = "..." }

Because there are many ways to solve a problem.
Post reply on HN