Home > Article > Backend Development > Overriding a function from another module in Golang
php editor Xigua is here to introduce you to an interesting topic: overriding a function from another module in Golang. In Golang, modular design is a common programming pattern, which makes the code easier to maintain and extend. Overriding functions is a powerful feature that allows us to override functions in one module in another module to achieve customized behavior. This article will explain in detail how to use the override function, as well as its benefits and considerations. Let’s explore this interesting topic together!
How to overwrite a function created in another module in golang?
Module a
In a module, I have the newpersonapiservice function, the complete code is as follows:
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") }
Module b
In a separate module, I want to override this newpersonapiservice.
I can call this function in other modules by doing the following:
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)) }
However, if I try to override the function, I get a compile error, the type of openapi cannot be resolved, here is what I try to do:
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 }
The following is a picture of the compilation error
other information: I believe module b correctly references module a.
The content of the go.mod file of module a is as follows:
module build/code/spec go 1.13 require github.com/go-chi/chi/v5 v5.0.3
The content of the go.mod file of module b is as follows:
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
The solution is to implement the showperson method in another module, you need to create a new type that implements the personapiservicer interface and provide its own implementation of the showperson method.
Running this code in module b works and allows me to change the response of the api call defined in module a.
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)) }
The above is the detailed content of Overriding a function from another module in Golang. For more information, please follow other related articles on the PHP Chinese website!