0

0

从 Golang 中的另一个模块覆盖函数

王林

王林

发布时间:2024-02-14 18:15:10

|

639人浏览过

|

来源于stackoverflow

转载

从 golang 中的另一个模块覆盖函数

php小编西瓜在这里为大家介绍一个有趣的话题:从Golang中的另一个模块覆盖函数。在Golang中,模块化的设计是一种常见的编程模式,它使代码更易于维护和扩展。覆盖函数是一个强大的特性,它允许我们在一个模块中重写另一个模块中的函数,从而实现自定义的行为。本文将详细介绍如何使用覆盖函数,以及它带来的好处和注意事项。让我们一起来探索这个有趣的话题吧!

问题内容

如何覆盖 golang 中另一个模块中创建的函数?

模块 a

在一个模块中,我有 newpersonapiservice 函数,完整代码如下:

立即学习go语言免费学习笔记(深入)”;

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。

我可以通过执行以下操作在其他模块中调用此函数:

Python之模块学习 中文WORD版
Python之模块学习 中文WORD版

本文档主要讲述的是Python之模块学习;python是由一系列的模块组成的,每个模块就是一个py为后缀的文件,同时模块也是一个命名空间,从而避免了变量名称冲突的问题。模块我们就可以理解为lib库,如果需要使用某个模块中的函数或对象,则要导入这个模块才可以使用,除了系统默认的模块(内置函数)不需要导入外。希望本文档会给有需要的朋友带来帮助;感兴趣的朋友可以过来看看

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

}

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
golang如何定义变量
golang如何定义变量

golang定义变量的方法:1、声明变量并赋予初始值“var age int =值”;2、声明变量但不赋初始值“var age int”;3、使用短变量声明“age :=值”等等。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

182

2024.02.23

golang有哪些数据转换方法
golang有哪些数据转换方法

golang数据转换方法:1、类型转换操作符;2、类型断言;3、字符串和数字之间的转换;4、JSON序列化和反序列化;5、使用标准库进行数据转换;6、使用第三方库进行数据转换;7、自定义数据转换函数。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

229

2024.02.23

golang常用库有哪些
golang常用库有哪些

golang常用库有:1、标准库;2、字符串处理库;3、网络库;4、加密库;5、压缩库;6、xml和json解析库;7、日期和时间库;8、数据库操作库;9、文件操作库;10、图像处理库。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

343

2024.02.23

golang和python的区别是什么
golang和python的区别是什么

golang和python的区别是:1、golang是一种编译型语言,而python是一种解释型语言;2、golang天生支持并发编程,而python对并发与并行的支持相对较弱等等。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

210

2024.03.05

golang是免费的吗
golang是免费的吗

golang是免费的。golang是google开发的一种静态强类型、编译型、并发型,并具有垃圾回收功能的开源编程语言,采用bsd开源协议。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

396

2024.05.21

golang结构体相关大全
golang结构体相关大全

本专题整合了golang结构体相关大全,想了解更多内容,请阅读专题下面的文章。

240

2025.06.09

golang相关判断方法
golang相关判断方法

本专题整合了golang相关判断方法,想了解更详细的相关内容,请阅读下面的文章。

194

2025.06.10

golang数组使用方法
golang数组使用方法

本专题整合了golang数组用法,想了解更多的相关内容,请阅读专题下面的文章。

458

2025.06.17

C++ 设计模式与软件架构
C++ 设计模式与软件架构

本专题深入讲解 C++ 中的常见设计模式与架构优化,包括单例模式、工厂模式、观察者模式、策略模式、命令模式等,结合实际案例展示如何在 C++ 项目中应用这些模式提升代码可维护性与扩展性。通过案例分析,帮助开发者掌握 如何运用设计模式构建高质量的软件架构,提升系统的灵活性与可扩展性。

9

2026.01.30

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Go 教程
Go 教程

共32课时 | 4.3万人学习

Laravel---API接口
Laravel---API接口

共7课时 | 0.6万人学习

Golang云原生架构师课程
Golang云原生架构师课程

共49课时 | 3.1万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号