wire.go文件:

// +build wireinject

// The build tag makes sure the stub is not built in the final build.

package di

import (

"github.com/google/wire"

)

//go:generate kratos t wire

func InitApp() (*App, func(), error) {

panic(wire.Build(dao.Provider, service.Provider, http.New, grpc.New, NewApp))

}

实现文件:

//dao

varProvider = wire.NewSet(New, NewDB, NewRedis)

//service

varProvider = wire.NewSet(New, wire.Bind(new(pb.Server), new(*Service)))

生成的wire_gen.go 文件:

func InitApp() (*App, func(), error) {

redis, cleanup, err := dao.NewRedis()

iferr != nil {

returnnil, nil, err

}

db, cleanup2, err := dao.NewDB()

iferr != nil {

cleanup()

returnnil, nil, err

}

daoDao, cleanup3, err := dao.New(redis, db)

iferr != nil {

cleanup2()

cleanup()

returnnil, nil, err

}

serviceService, cleanup4, err := service.New(daoDao)

iferr != nil {

cleanup3()

cleanup2()

cleanup()

returnnil, nil, err

}

engine, err := http.New(serviceService)

iferr != nil {

cleanup4()

cleanup3()

cleanup2()

cleanup()

returnnil, nil, err

}

server, err := grpc.New(serviceService)

iferr != nil {

cleanup4()

cleanup3()

cleanup2()

cleanup()

returnnil, nil, err

}

app, cleanup5, err := NewApp(serviceService, engine, server)

iferr != nil {

cleanup4()

cleanup3()

cleanup2()

cleanup()

returnnil, nil, err

}

returnapp, func() {

cleanup5()

cleanup4()

cleanup3()

cleanup2()

cleanup()

}, nil

}