I am coming from PHP OOP background so just want to find out if what I do with Go is common/acceptable practise or not (I am newbie in Go). It is to do with Dependency Injection.
In OOP it is ideal to Dependency Inject object X into object Y rather than directly instantiating object X in object Y. I won't list the reasons because I believe most of you know general OOP practises etc. Given that Go is not an OOP language and I've seen both versions being used, which version below wins over another an why?
api/internal/config/config.go
package config
type Config struct
{
Locale string
}
func NewConfig() Config {
return Config{"en"}
}
VER 1
api/internal/app/app.go
package app
func Start(c config.Config) {
fmt.Println(c.Locale)
}
cmd/main.go
package main
import (
"api/internal/app"
"api/internal/config"
)
func main() {
c := config.NewConfig()
app.Start(c)
}
VER 2
api/internal/app/app.go
package app
import (
"fmt"
"api/internal/config"
)
func Start() {
c := config.NewConfig()
fmt.Println(c.Locale)
}
cmd/main.go
package main
import "api/internal/app"
func main() {
app.Start()
}