1 安装wire
1.1 通过命令直接安装
go install github.com/google/wire/cmd/wire@latest
1.2 通过go get方式安装
go get github.com/google/wire/cmd/wire
进入目录编译
cd C:\Users\leell\go\pkg\mod\github.com\google\wire@v0.6.0\cmd\wire
go build
然后将wire.exe移动到 C:\Users\leell\go\bin目录下
具体目录通过go env进行查看
C:\Users\leell\go>go env
set GO111MODULE=on
set GOARCH=amd64
set GOBIN=C:\Users\leell\go\bin
2 业务源码
//Handler 源码
type Handler struct {
conf *viper.Viper
logger *zap.Logger
}
func NewHandler(conf *viper.Viper, logger *zap.Logger) *Handler {
return &Handler{conf: conf, logger: logger}
}
//RoleHandler 源码
type RoleHandler struct {
*Handler
roleService *service.RoleService
}
func NewRoleController(controller *Handler, roleService *service.RoleService) *RoleHandler {
return &RoleHandler{
Handler: controller,
roleService: roleService,
}
}
//RoleService 源码
type RoleService struct {
*Service
roleDao *dao.RoleDao
}
func NewRoleService(service *Service, roleDao *dao.RoleDao) *RoleService {
return &RoleService{
Service: service,
roleDao: roleDao,
}
}
//RoleDao源码
type RoleDao struct {
*Dao
}
func NewRoleDao(dao *Dao) *RoleDao {
return &RoleDao{Dao: dao}
}
//Dao 源码
type Dao struct {
conf *viper.Viper
logger *zap.Logger
}
func NewDao(conf *viper.Viper, logger *zap.Logger) *Dao {
return &Dao{conf: conf, logger: logger}
}
3 provider 提供者
provider/provider.go
package provider
import (
"github.com/google/wire"
"wire-first/dao"
"wire-first/handler"
"wire-first/server"
"wire-first/service"
)
var ServerSet = wire.NewSet(server.NewServerHttp)
var HandlerSet = wire.NewSet(handler.NewHandler, handler.NewUserController, handler.NewRoleController)
var ServiceSet = wire.NewSet(service.NewService, service.NewUserService, service.NewRoleService)
var DaoSet = wire.NewSet(dao.NewDao, dao.NewUserDao, dao.NewRoleDao)
4 wire提供生成构建代码
wire/wire.go
//go:build wireinject
// +build wireinject
package wire
import (
"net/http"
"github.com/google/wire"
"github.com/spf13/viper"
"go.uber.org/zap"
"wire-first/provider"
)
// wire.go 初始化模块
func NewApp(*viper.Viper, *zap.Logger) (*http.Server, error) {
panic(wire.Build(
provider.ServerSet,
provider.HandlerSet,
provider.ServiceSet,
provider.DaoSet,
))
}
5 通过wire生成gen代码
PS G:\github\wire-first> wire gen wire/wire.go
wire: command-line-arguments: wrote G:\github\wire-first\wire\wire_gen.go
6 结果查看
wire/wire_gen.go
// Code generated by Wire. DO NOT EDIT.
//go:generate go run -mod=mod github.com/google/wire/cmd/wire
//go:build !wireinject
// +build !wireinject
package wire
import (
"github.com/spf13/viper"
"go.uber.org/zap"
"net/http"
"wire-first/dao"
"wire-first/handler"
"wire-first/server"
"wire-first/service"
)
// Injectors from wire.go:
// wire.go 初始化模块
func NewApp(viperViper *viper.Viper, logger *zap.Logger) (*http.Server, error) {
handlerHandler := handler.NewHandler(viperViper, logger)
serviceService := service.NewService(viperViper, logger)
daoDao := dao.NewDao(viperViper, logger)
userDao := dao.NewUserDao(daoDao)
userService := service.NewUserService(serviceService, userDao)
userHandler := handler.NewUserController(handlerHandler, userService)
roleDao := dao.NewRoleDao(daoDao)
roleService := service.NewRoleService(serviceService, roleDao)
roleHandler := handler.NewRoleController(handlerHandler, roleService)
httpServer := server.NewServerHttp(viperViper, logger, userHandler, roleHandler)
return httpServer, nil
}
7 main.go代码
package main
import (
"github.com/alecthomas/kingpin/v2"
"go.uber.org/zap"
"wire-first/config"
"wire-first/wire"
)
var (
cfgPath = kingpin.Flag("config", "the path of the config file").Default("app.yml").String()
)
func main() {
kingpin.Parse()
conf := config.NewConfig(*cfgPath)
// 创建logger
logger, err := zap.NewDevelopment()
if err != nil {
panic(err)
}
defer logger.Sync()
app, err := wire.NewApp(conf, logger)
if err != nil {
logger.Error("Initialization failed",
zap.Error(err),
)
}
logger.Info("Server's running", zap.String("address", app.Addr))
if err := app.ListenAndServe(); err != nil {
logger.Error("Server Error",
zap.String("key", "value"),
zap.Error(err),
)
}
}