Skip to content

Instantly share code, notes, and snippets.

@Nydan
Last active December 30, 2018 04:31
Show Gist options
  • Save Nydan/5753d15657e8e978958cb1204bb097d1 to your computer and use it in GitHub Desktop.
Save Nydan/5753d15657e8e978958cb1204bb097d1 to your computer and use it in GitHub Desktop.
Constructor for dependency injection
import "database/sql"
// Resource is type that being used as an object to receive dependency
type Resource struct {
db *sql.DB
}
// New is constructor function that do the injection to Resource
func New(db *sql.DB) *Resource {
r := &Resource{
db: db,
}
return r
}
// PingDB is method receiver from type Resource which using database injection from constructor func
func (r *Resource) PingDB() error {
return r.db.Ping()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment