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)) }
登錄后復制