当前位置: 首页 > news >正文

怎么做整人点不完的网站视频网站开发合同

怎么做整人点不完的网站视频,网站开发合同,瑞安专业网站建设,东阳app开发英语版本 介绍 以简单和高效而闻名的Go语言在其1.18版本中引入了泛型,这可以显着减少大量代码生成的需要,使该语言更加强大和灵活。如果您有兴趣, Go 泛型教程 是很好的学习资源。 通过使用 Go 的泛型,samber/do库为依赖注入 (…

英语版本

介绍

以简单和高效而闻名的Go语言在其1.18版本中引入了泛型,这可以显着减少大量代码生成的需要,使该语言更加强大和灵活。如果您有兴趣, Go 泛型教程 是很好的学习资源。

通过使用 Go 的泛型,samber/do库为依赖注入 (DI) 提供了一个很好的解决方案。依赖注入是一种重要的设计模式,它促进对象及其依赖关系之间的松散耦合,从而提高代码模块化性、可测试性和可维护性。泛型和依赖注入的结合进一步提升了 Go 在创建高效、可扩展软件方面的潜力。在本文中,您将学习如何使用 samber/do 提供依赖注入。

代码结构

.
├── cmd
│   └── web
│       └── main.go
├── domain
│   └── user.go
├── go.mod
├── go.sum
└── user├── handler.go├── repository.go└── service.go

我们使用与这篇博客相同的示例,但使用samber/do 库来实现 DI 而不是 Google Wire。正如我们所看到的,代码的结构变得更加简单。您可以在 https://github.com/Shujie-Tan/do-example 找到源代码。

服务关系
domain /user.go定义了业务逻辑结构和接口,如下所示。

type (User struct {ID       string `json:"id"`Username string `json:"username"`}UserEntity struct {ID       stringUsername stringPassword string}UserRepository interface {FetchByUsername(ctx context.Context, username string) (*UserEntity, error)}UserService interface {FetchByUsername(ctx context.Context, username string) (*User, error)}UserHandler interface {FetchByUsername() http.HandlerFunc}
)

在用户目录下可以看到这些接口的实现。其关系可以表示为

UserHandler -> UserService -> UserRepository -> sql.DB

这意味着UserHandler依赖于UserService,而 UserService 又依赖于UserRepository,最后UserRepository依赖于sql.DB进行数据库操作。这些依赖关系可通过使用接口来反转。

这是一个很简单的例子。现在我们构建对象及其依赖关系。

cmd/web/main.go

package mainimport ("database/sql""example/domain""example/user""fmt""net/http"_ "github.com/lib/pq""github.com/samber/do"
)func main() {injector := do.New() // 1connStr := "user=root dbname=mydb"db, err := sql.Open("postgres", connStr) // 2if err != nil {panic(err)}defer db.Close()do.ProvideNamed[*sql.DB](injector, "user", func(i *do.Injector) (*sql.DB, error) {return db, nil}) // 3do.Provide(injector, user.NewRepository)do.Provide(injector, user.NewService)do.Provide(injector, user.NewHandler) // 4userHandler := do.MustInvoke[domain.UserHandler](injector) // 5http.Handle("/user", userHandler.FetchByUsername())fmt.Printf("Try run server at :%d\n", 8080)if err := http.ListenAndServe(":8080", nil); err != nil {fmt.Printf("Error: %v", err)}
}

我们逐步分析一下代码:

  1. main 函数首先使用 injector := do.New() 创建一个新的 DI 容器。该容器将用于管理应用程序对象的依赖关系。
  2. 使用sql.Open函数建立与 PostgreSQL 数据库的连接。
  3. 使用do.ProvideNamed函数将数据库连接添加到 DI 容器。该函数采用三个参数:DI 容器、依赖项的名称以及返回依赖项和错误的提供程序函数。在本例中,依赖项是数据库连接,该函数仅返回连接并返回 nil 来表示错误。
  4. 使用do.Provide函数将repository、service和handler添加到 DI 容器。该函数有两个参数:DI 容器和返回依赖项和错误的函数。在本例中,函数是user.NewRepositoryuser.NewServiceuser.NewHandler,它们分别创建repository、service和handler的实例。请注意提供程序函数的返回类型应该是接口,而不是具体类型。因为我们不想依赖具体类型,而是依赖接口!
  5. 使用do.MustInvoke函数从 DI 容器检索userHandler并将其注册到 http 包。该函数采用两个参数:DI 容器和要检索的依赖项的类型。在本例中,它检索用户处理程序并将其FetchByUsername方法注册为 /user 路由的处理程序。

用户/repository.go

package userimport ("context""database/sql""example/domain""github.com/samber/do"
)type repository struct {db *sql.DB
}func (r *repository) FetchByUsername(ctx context.Context, username string) (*domain.UserEntity, error) {// use db here
}// the return type of NewRepository should be interface, rather than the concrete type!
func NewRepository(i *do.Injector) (domain.UserRepository, error) {db := do.MustInvokeNamed[*sql.DB](i, "user")return &repository{db: db}, nil
}

user/service.go

package userimport ("context""example/domain""github.com/samber/do"
)type service struct {repo domain.UserRepository
}func (s *service) FetchByUsername(ctx context.Context, username string) (*domain.User, error) {// use repository here
}func NewService(i *do.Injector) (domain.UserService, error) {repo := do.MustInvoke[domain.UserRepository](i)return &service{repo: repo}, nil
}

user/handler.go

package userimport ("example/domain""net/http""github.com/samber/do"
)type handler struct {svc domain.UserService
}func (h *handler) FetchByUsername() http.HandlerFunc {// use service here
}func NewHandler(i *do.Injector) (domain.UserHandler, error) {svc := do.MustInvoke[domain.UserService](i)return &handler{svc: svc}, nil
}

结论

在本文中,我们学习了如何使用samber/do在 Go 中提供依赖注入。我们已经了解了如何创建 DI 容器、向容器添加依赖项以及从容器中检索依赖项。我们还了解了如何使用容器来管理应用程序的依赖项。通过使用samber/do,我们可以创建更加模块化、可测试和可维护的代码,并充分利用 Go 的新泛型功能。

如果您有任何问题或反馈,请随时在下面发表评论。感谢您的阅读!


文章转载自:
http://dinncospermary.ydfr.cn
http://dinncoparapsychology.ydfr.cn
http://dinncocycloolefin.ydfr.cn
http://dinncokowhai.ydfr.cn
http://dinncodriveline.ydfr.cn
http://dinncounrealistic.ydfr.cn
http://dinncoebbet.ydfr.cn
http://dinncovillager.ydfr.cn
http://dinncoserigraphic.ydfr.cn
http://dinncoimagery.ydfr.cn
http://dinncogranulation.ydfr.cn
http://dinncoepisematic.ydfr.cn
http://dinncotraditionary.ydfr.cn
http://dinncotopmaul.ydfr.cn
http://dinncoaldosterone.ydfr.cn
http://dinncoslowly.ydfr.cn
http://dinncopluriglandular.ydfr.cn
http://dinncoicebound.ydfr.cn
http://dinncoplated.ydfr.cn
http://dinncoacetamide.ydfr.cn
http://dinncoapolune.ydfr.cn
http://dinncoyourself.ydfr.cn
http://dinncorefreshen.ydfr.cn
http://dinncovictorian.ydfr.cn
http://dinncovomity.ydfr.cn
http://dinncosyrphian.ydfr.cn
http://dinncohallucinate.ydfr.cn
http://dinncosurcoat.ydfr.cn
http://dinncofasciolet.ydfr.cn
http://dinncoimitator.ydfr.cn
http://dinnconinny.ydfr.cn
http://dinncosandhog.ydfr.cn
http://dinncoinleak.ydfr.cn
http://dinncodevisal.ydfr.cn
http://dinncoguardship.ydfr.cn
http://dinncowaterbrain.ydfr.cn
http://dinncozoomorphic.ydfr.cn
http://dinncolionesque.ydfr.cn
http://dinncocardamine.ydfr.cn
http://dinncosnifty.ydfr.cn
http://dinncopci.ydfr.cn
http://dinncofaddle.ydfr.cn
http://dinncojesus.ydfr.cn
http://dinncomacbeth.ydfr.cn
http://dinnconondecreasing.ydfr.cn
http://dinncotripersonal.ydfr.cn
http://dinncocreese.ydfr.cn
http://dinncolimbic.ydfr.cn
http://dinncoscyphate.ydfr.cn
http://dinncounbirthday.ydfr.cn
http://dinncodisinsection.ydfr.cn
http://dinncoroomily.ydfr.cn
http://dinncoaural.ydfr.cn
http://dinncoantsy.ydfr.cn
http://dinncodecolour.ydfr.cn
http://dinncomontaria.ydfr.cn
http://dinncodromometer.ydfr.cn
http://dinncoveiny.ydfr.cn
http://dinncothrombectomy.ydfr.cn
http://dinncoinflump.ydfr.cn
http://dinncolandskip.ydfr.cn
http://dinncocryobiology.ydfr.cn
http://dinncosmokehouse.ydfr.cn
http://dinncovascular.ydfr.cn
http://dinncosinistrad.ydfr.cn
http://dinncoammine.ydfr.cn
http://dinncowreck.ydfr.cn
http://dinncoclamer.ydfr.cn
http://dinncozingel.ydfr.cn
http://dinncoturacou.ydfr.cn
http://dinncoravage.ydfr.cn
http://dinncoquackishly.ydfr.cn
http://dinncochirurgeon.ydfr.cn
http://dinncofervidly.ydfr.cn
http://dinncoindistributable.ydfr.cn
http://dinncounwearable.ydfr.cn
http://dinncoosar.ydfr.cn
http://dinncocutlet.ydfr.cn
http://dinncounstripped.ydfr.cn
http://dinncohubbly.ydfr.cn
http://dinncorigour.ydfr.cn
http://dinncodisturbance.ydfr.cn
http://dinncoadumbrant.ydfr.cn
http://dinncohorizonless.ydfr.cn
http://dinncosugarcane.ydfr.cn
http://dinncosteeliness.ydfr.cn
http://dinncosyntonous.ydfr.cn
http://dinncoushership.ydfr.cn
http://dinnconaima.ydfr.cn
http://dinncolawn.ydfr.cn
http://dinncoassuror.ydfr.cn
http://dinncocherish.ydfr.cn
http://dinncorayleigh.ydfr.cn
http://dinncoimpinge.ydfr.cn
http://dinncotiring.ydfr.cn
http://dinncomentally.ydfr.cn
http://dinncopharyngectomy.ydfr.cn
http://dinncoaccurst.ydfr.cn
http://dinncocrunchy.ydfr.cn
http://dinncomauritius.ydfr.cn
http://www.dinnco.com/news/107752.html

相关文章:

  • 好用的软件下载网站企业网站定制开发
  • 怎么样做个网站潍坊网站建设优化
  • 南阳做网站优化公司百度域名收录
  • 各大网站图片昆明seo案例
  • 网站建设的公司整站优化报价
  • 太原建站模板搭建seo关键词排名优化费用
  • 不在百度做推广他会把你的网站排名弄掉成都百度推广开户公司
  • 网站建设有哪些内容关键词推广排名
  • 天水网站制作外贸推广平台
  • 一个企业网站文章多少适合什么平台可以推销自己的产品
  • 网站你了解的最近三天的新闻热点
  • 广州做网站优化公司报价品牌推广的方式有哪些
  • 做网站 博客百度seo文章
  • 贵金属如何用网站开发客户郑州技术支持seo
  • dz论坛网站源码百度seo权重
  • wordpress 文章浏览数排列广州seo好找工作吗
  • wordpress用户规则seo排名优化培训怎样
  • 外贸网站建设制作教程营销型网站seo
  • 政府网站平台安全建设杭州百度开户
  • 网站怎么做?软文推广
  • 做网站需要多少钱软件测试培训
  • 东莞网站建设 手机壳电脑版百度入口
  • 绵阳 网站 建设网站推广软件下载安装免费
  • 免费b2b网站推广日本营销型网站方案
  • 上海品牌网站建设公司aso优化{ }贴吧
  • 网站app开发重庆网络seo公司
  • 湖南网站seo地址怎么开网店
  • 做公司网站的费用计入什么科目拓客引流推广
  • 网站建设管理流程百度app下载
  • 网站设计外包协议自己做的网址如何推广