从 Golang 中的另一个模块覆盖函数
php小编西瓜在这里为大家介绍一个有趣的话题:从Golang中的另一个模块覆盖函数。在Golang中,模块化的设计是一种常见的编程模式,它使代码更易于维护和扩展。覆盖函数是一个强大的特性,它允许我们在一个模块中重写另一个模块中的函数,从而实现自定义的行为。本文将详细介绍如何使用覆盖函数,以及它带来的好处和注意事项。让我们一起来探索这个有趣的话题吧!
问题内容
如何覆盖 golang 中另一个模块中创建的函数?
模块 a
在一个模块中,我有 newpersonapiservice 函数,完整代码如下:
package openapi import ( "context" "errors" "net/http" ) // personapiservice is a service that implements the logic for the personapiservicer // this service should implement the business logic for every endpoint for the personapi api. // include any external packages or services that will be required by this service. type personapiservice struct { } // newpersonapiservice creates a default api service func newpersonapiservice() personapiservicer { return &personapiservice{} } // showperson - detail func (s *personapiservice) showperson(ctx context.context) (implresponse, error) { // todo - update showperson with the required logic for this service method. // add api_person_service.go to the .openapi-generator-ignore to avoid overwriting this service implementation when updating open api generation. //todo: uncomment the next line to return response response(200, person{}) or use other options such as http.ok ... //return response(200, person{}), nil //todo: uncomment the next line to return response response(0, error{}) or use other options such as http.ok ... //return response(0, error{}), nil return response(http.statusnotimplemented, nil), errors.new("showperson method not implemented") }
模块 b
在一个单独的模块中,我想覆盖这个 newpersonapiservice。
我可以通过执行以下操作在其他模块中调用此函数:
package main import ( "log" "net/http" openapi "build/code/spec/src" ) func main() { log.printf("server started") personapiservice := openapi.newpersonapiservice() personapicontroller := openapi.newpersonapicontroller(personapiservice) router := openapi.newrouter(personapicontroller) log.fatal(http.listenandserve(":8080", router)) }
但是,如果我尝试覆盖该函数,则会出现编译错误,openapi 的类型无法解析,以下是我尝试执行的操作:
package main import ( "context" "log" "net/http" openapi "build/code/spec/src" ) func main() { log.printf("server started") personapiservice := openapi.newpersonapiservice() personapicontroller := openapi.newpersonapicontroller(personapiservice) router := openapi.newrouter(personapicontroller) log.fatal(http.listenandserve(":8080", router)) } func (s openapi.personapiservice) showperson(ctx context.context) (openapi.implresponse, error) { return openapi.response(200, openapi.person{}), nil }
下面是编译错误的图片
其他信息: 我相信模块 b 正确引用了模块 a。
模块a的go.mod文件内容如下:
module build/code/spec go 1.13 require github.com/go-chi/chi/v5 v5.0.3
模块b的go.mod文件内容如下:
module bakkt.com/boilerplate go 1.19 replace build/code/spec => ./../build/generated/ require build/code/spec v0.0.0-00010101000000-000000000000 require github.com/go-chi/chi/v5 v5.0.3 // indirect
解决方法
解决方案是在另一个模块中实现 showperson 方法,您需要创建一个新类型来实现 personapiservicer 接口并提供其自己的 showperson 方法的实现。
在模块 b 中运行此代码有效,并允许我更改模块 a 中定义的 api 调用的响应。
package main import ( "context" "log" "net/http" openapi "build/code/spec/src" ) type MyPersonApiService struct{} func NewMyPersonApiService() openapi.PersonApiServicer { return &MyPersonApiService{} } func (s *MyPersonApiService) ShowPerson(ctx context.Context) (openapi.ImplResponse, error) { // TODO: Add your own implementation of the ShowPerson method here. // For example, you could retrieve a person's details and return them as follows: person := openapi.Person{Id: 23, Name: "Vark Thins", Age: 20} return openapi.Response(http.StatusOK, person), nil } func main() { log.Printf("Server started") PersonApiService := NewMyPersonApiService() PersonApiController := openapi.NewPersonApiController(PersonApiService) router := openapi.NewRouter(PersonApiController) log.Fatal(http.ListenAndServe(":8080", router)) }
以上是从 Golang 中的另一个模块覆盖函数的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

在函数继承中,使用“基类指针”和“派生类指针”来理解继承机制:基类指针指向派生类对象时,执行向上转型,只访问基类成员。派生类指针指向基类对象时,执行向下转型(不安全),必须谨慎使用。

Notepad++ 本身不能运行 C 语言程序,需要一个外部编译器来编译和执行代码。为了使用外部编译器,可以按以下步骤进行设置:1. 下载并安装 C 语言编译器;2. 在 Notepad++ 中创建自定义工具,将编译器可执行文件路径和参数配置好;3. 创建 C 语言程序并保存为 .c 文件扩展名;4. 选择 C 语言程序文件,从“运行”菜单中选择自定义工具进行编译;5. 查看编译结果,输出编译错误或成功消息。如果编译成功,将生成可执行文件。

在 Sublime Text 中运行 Python 代码的快捷键为:Windows 和 Linux: Ctrl + BMac: Cmd + B将光标放置在代码中。按下快捷键。代码将使用系统默认的 Python 解释器运行。

Java 中的 val 关键字用于声明不可变的局部变量,即一旦赋值后无法更改其值。特点有:不可变性:一旦初始化,val 变量不能重新赋值。局部作用域:val 变量仅在声明它们的代码块内可见。类型推断:Java 编译器会根据赋值的表达式推断 val 变量的类型。仅限局部变量:val 只能用于声明局部变量,不能用于类字段或方法参数。

const 修饰符表示常量,值不可修改;static 修饰符指示变量的生存期和作用域。const 修饰的数据成员在初始化后不可修改,static 修饰的变量在程序启动时初始化,在程序结束时销毁,即使没有活动对象也会存在,并且可以跨函数访问。const 修饰局部变量必须声明时初始化,static 修饰的局部变量可以稍后初始化。const 修饰类成员变量必须在构造函数或初始化列表中初始化,static 修饰的类成员变量可以在类外部初始化。

Java 编程语言中的 "=" 操作符用于给变量赋值,将表达式右侧的值存储在左侧变量中。用法:变量 = 表达式,其中变量是接收赋值的变量名称,表达式是计算或返回值的代码段。

为了在Java泛型方法中限制类型参数,需使用语法,其中Bound为类型或接口。如此,参数仅接受继承自Bound类型或实现Bound接口的类型。例如,限制T为可与自身比较的类型。

编译器区分重载函数的方法:通过签名,即每个函数参数的类型。即使函数名称和参数数量相同,只要参数类型不同,编译器就能区分开来。
