Singleton vs Instances

292 views
Skip to first unread message

Abhinandan NM

unread,
May 10, 2015, 3:13:50 PM5/10/15
to golan...@googlegroups.com
I have two patterns in my mind. Can you guys tell me which one is better? and why?

Pattern 1.

package main

import "System"

func main
() {
 
System.boot()
}

package System

import (
  "github.com/codegangsta/cli"
  "github.com/gorilla/mux"
)

type app
struct {
 
Cmd        *cli.App
  HTTPRouter *mux.Router
  Ses        *mgo.Session
}

// App is a singleton throughout the application
var App = new(app)

// Boot bootstraps the application
func
Boot() {
  App.Cmd = cli.NewApp()
  App.HTTPRouter = mux.NewRouter()
  App.Ses = mgo.Dial("mongodb://user:pa...@server.compose.io/db_name")
}


Pattern 2.

package main

import "app"

func main
() {
  a := app.NewApp()
  
}


package app

import (
  "github.com/codegangsta/cli"
  "github.com/gorilla/mux"
  "gopkg.in/mgo.v2"
)

type components 
struct {
  
Cmd        *cli.App
  HTTPRouter *mux.Router
  Ses        *mgo.Session
}

// NewApp provides new app instance
func 
NewApp() *app {
  c := new(components)
  c.Cmd = cli.NewApp()
  c.HTTPRouter = mux.NewRouter()
  c.Ses = mgo.Dial("mongodb://user:pa...@server.compose.io/db_name")
a := new(app)
return c, a
}



In first pattern, the app servers like a singleton. In the second pattern, I make new instances of the app whenever I require it. Which one is better?

Tamás Gulácsi

unread,
May 10, 2015, 4:27:25 PM5/10/15
to golan...@googlegroups.com
Better for what? You haven't stated your goals. Why do you want a singleton? If you want a Singleton, can't you create it in init() ?

Abhinandan NM

unread,
May 11, 2015, 1:39:27 AM5/11/15
to golan...@googlegroups.com
Well, is it better to have single instances of things like mongo sessions throughout the app or is it better to create and close it whenever I want it?

Henrik Johansson

unread,
May 11, 2015, 1:53:17 AM5/11/15
to Abhinandan NM, golan...@googlegroups.com

That depends on the things. Not all things work the same way. You may want to factor in other aspects such as how you want to test your code and general maintainability as well.


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jesse McNelis

unread,
May 11, 2015, 2:14:01 AM5/11/15
to Abhinandan NM, golang-nuts
On Mon, May 11, 2015 at 3:39 PM, Abhinandan NM <nmabhi...@gmail.com> wrote:
> Well, is it better to have single instances of things like mongo sessions
> throughout the app or is it better to create and close it whenever I want
> it?

If you look at the standard library there are many example of packages
that do both.

The net/http package has a http.Client called DefaultClient which all
the package level functions use.
eg. http.Get() just calls http.DefaultClient.Get()

But the DefaultClient is just an instance of http.Client.


So, callers with simple needs can conveniently the DefaultClient, and
callers with complex needs can instantiate their own http.Client.
Reply all
Reply to author
Forward
0 new messages