how golang wire use struct embeding?
config.go
type Config struct {
Database string `json:"database"`
Mysql MySQLConfig `json:"mysql"`
}
type MySQLConfig struct {
Host string `json:"host"`
Port int64 `json:"port"`
}
func NewConfig() (*Config, error) {
fp, err := os.Open("../config/app.json")
if err != nil {
return nil, err
}
defer fp.Close()
var cfg Config
if err := json.NewDecoder(fp).Decode(&cfg); err != nil {
return nil, err
}
return &cfg, nil
}
mysql.go
type MySQL struct {
}
func NewDB(cfg MySQLConfig) (*MySQL, error) {
fmt.Printf("mysql config: %#vn", cfg)
return &MySQL{}, nil
}
wire.go
func InitAPP() (*MySQL, error) {
wire.Build(NewConfig, NewDB)
return &MySQL{}, nil
}
error:
wire.go:10:1: inject InitAPP: no provider found for demo/demo6_cfg.MySQLConfig
needed by *demo/demo6_cfg.MySQL in provider “NewDB” (/demo6_cfg/mysql.go:10:6)
wire: demo/demo6_cfg: generate failed
what should I fix this error?
New contributor
verymiao is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1