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中文網其他相關文章!