Last active
December 30, 2018 04:31
-
-
Save Nydan/5753d15657e8e978958cb1204bb097d1 to your computer and use it in GitHub Desktop.
Constructor for dependency injection
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
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