php小編西瓜在這里為大家介紹一個(gè)有趣的話題:從Golang中的另一個(gè)模塊覆蓋函數(shù)。在Golang中,模塊化的設(shè)計(jì)是一種常見的編程模式,它使代碼更易于維護(hù)和擴(kuò)展。覆蓋函數(shù)是一個(gè)強(qiáng)大的特性,它允許我們?cè)谝粋€(gè)模塊中重寫另一個(gè)模塊中的函數(shù),從而實(shí)現(xiàn)自定義的行為。本文將詳細(xì)介紹如何使用覆蓋函數(shù),以及它帶來的好處和注意事項(xiàng)。讓我們一起來探索這個(gè)有趣的話題吧!
問題內(nèi)容
如何覆蓋 golang 中另一個(gè)模塊中創(chuàng)建的函數(shù)?
模塊 a
在一個(gè)模塊中,我有 newpersonapiservice 函數(shù),完整代碼如下:
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") }
登錄后復(fù)制
模塊 b
在一個(gè)單獨(dú)的模塊中,我想覆蓋這個(gè) newpersonapiservice。
我可以通過執(zhí)行以下操作在其他模塊中調(diào)用此函數(shù):
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)) }
登錄后復(fù)制
但是,如果我嘗試覆蓋該函數(shù),則會(huì)出現(xiàn)編譯錯(cuò)誤,openapi 的類型無法解析,以下是我嘗試執(zhí)行的操作:
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 }
登錄后復(fù)制
下面是編譯錯(cuò)誤的圖片
其他信息:
我相信模塊 b 正確引用了模塊 a。
模塊a的go.mod文件內(nèi)容如下:
module build/code/spec go 1.13 require github.com/go-chi/chi/v5 v5.0.3
登錄后復(fù)制
模塊b的go.mod文件內(nèi)容如下:
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
登錄后復(fù)制
解決方法
解決方案是在另一個(gè)模塊中實(shí)現(xiàn) showperson 方法,您需要?jiǎng)?chuàng)建一個(gè)新類型來實(shí)現(xiàn) personapiservicer 接口并提供其自己的 showperson 方法的實(shí)現(xiàn)。
在模塊 b 中運(yùn)行此代碼有效,并允許我更改模塊 a 中定義的 api 調(diào)用的響應(yīng)。
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)) }
登錄后復(fù)制