登录
首页 >  Golang >  Go问答

从 Golang 中的另一个模块覆盖函数

来源:stackoverflow

时间:2024-02-12 15:18:25 317浏览 收藏

目前golang学习网上已经有很多关于Golang的文章了,自己在初次阅读这些文章中,也见识到了很多学习思路;那么本文《从 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 中的另一个模块覆盖函数》的内容介绍就到此结束,如果有什么疑问或者建议,可以在golang学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>