日日操夜夜添-日日操影院-日日草夜夜操-日日干干-精品一区二区三区波多野结衣-精品一区二区三区高清免费不卡

公告:魔扣目錄網(wǎng)為廣大站長(zhǎng)提供免費(fèi)收錄網(wǎng)站服務(wù),提交前請(qǐng)做好本站友鏈:【 網(wǎng)站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(wù)(50元/站),

點(diǎn)擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會(huì)員:747

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

分享到:
標(biāo)簽:編譯錯(cuò)誤
用戶無頭像

網(wǎng)友整理

注冊(cè)時(shí)間:

網(wǎng)站:5 個(gè)   小程序:0 個(gè)  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會(huì)員

趕快注冊(cè)賬號(hào),推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨(dú)大挑戰(zhàn)2018-06-03

數(shù)獨(dú)一種數(shù)學(xué)游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫(kù),初中,高中,大學(xué)四六

運(yùn)動(dòng)步數(shù)有氧達(dá)人2018-06-03

記錄運(yùn)動(dòng)步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓(xùn)練成績(jī)?cè)u(píng)定2018-06-03

通用課目體育訓(xùn)練成績(jī)?cè)u(píng)定