亚洲视频二区_亚洲欧洲日本天天堂在线观看_日韩一区二区在线观看_中文字幕不卡一区

公告:魔扣目錄網為廣大站長提供免費收錄網站服務,提交前請做好本站友鏈:【 網站目錄:http://www.430618.com 】, 免友鏈快審服務(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

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))

}

登錄后復制

分享到:
標簽:編譯錯誤
用戶無頭像

網友整理

注冊時間:

網站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網站吧!
最新入駐小程序

數獨大挑戰2018-06-03

數獨一種數學游戲,玩家需要根據9

答題星2018-06-03

您可以通過答題星輕松地創建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學四六

運動步數有氧達人2018-06-03

記錄運動步數,積累氧氣值。還可偷

每日養生app2018-06-03

每日養生,天天健康

體育訓練成績評定2018-06-03

通用課目體育訓練成績評定