可变参数类型在函数中的使用
来源:stackoverflow
时间:2024-02-09 21:30:24 464浏览 收藏
学习Golang要努力,但是不要急!今天的这篇文章《可变参数类型在函数中的使用》将会介绍到等等知识点,如果你想深入学习Golang,可以关注我!我会持续更新相关文章的,希望对大家都能有所帮助!
我创建了一个函数来获取用户对拉取请求的最后评论。我正在使用“github.com/google/go-github/github”包。我想将它用于 []*github.issuecomment 和 []*github.pullrequestcomment 类型。有没有办法使输入参数的类型可变,这样我就不必在函数定义中指定它,并且可以使用任一类型调用函数?
func getlastuserinteractionpr(comments_array *github.issuecomment or *github.pullrequestcomment)(*github.issuecomment or *github.pullrequestcomment) { }
泛型的使用:
func getlastuserinteractionpr(comments_array any)(any) { }
这是一个紧急解决方案,因为我正在做的整个项目都是用 go 1.14 编写的,并且这个功能可以从 go 1.18 中获得
当我尝试使用空接口{}作为输入类型时:
func getLastUserInteractionPRIssue(comments_array interface{})(*github.IssueComment) { comments_array []*github.IssueComment(comments_array); err { fmt.Println("success") } else { fmt.Println("failure") } }
正确答案
你关心例如的内部结构吗? issuecomment
?
type issuecomment struct { id *int64 `json:"id,omitempty"` nodeid *string `json:"node_id,omitempty"` body *string `json:"body,omitempty"` user *user `json:"user,omitempty"` reactions *reactions `json:"reactions,omitempty"` createdat *time.time `json:"created_at,omitempty"` updatedat *time.time `json:"updated_at,omitempty"` // authorassociation is the comment author's relationship to the issue's repository. // possible values are "collaborator", "contributor", "first_timer", "first_time_contributor", "member", "owner", or "none". authorassociation *string `json:"author_association,omitempty"` url *string `json:"url,omitempty"` htmlurl *string `json:"html_url,omitempty"` issueurl *string `json:"issue_url,omitempty"` }
例如,您关心从中提取某些特定字段吗? pullrequestcomment
是一个更大的结构(它有更多字段),您关心从中提取一些字段吗?
或者您只想要每个的字符串表示形式?您要做什么很大程度上取决于您想如何处理传递的值。
如果您只想要每个 string
表示,您可以使用极端(老实说,不是很安全 - 我不推荐这个)示例,让您的函数接受 fmt.stringer
对象的切片: p>
func dostuffwithstringifiedcomments(cs []fmt.stringer) { // both issuecomment and pullrequestcomment provide string() // methods and therefore implement fmt.stringer for _, comment := range cs { dosomethingwith(comment.string()) } }
您的切片现在可以包含任一类型的对象,并且不会发生任何爆炸。缺点:它还可能包含数以亿计的其他类型,但没有一个是您想要的。因此,您需要添加类型断言检查:
switch t := comment.(type) { case github.issuecomment: // do good stuff case github.pullrequestcomment: // do other good stuff default: // yell and scream about the value of t }
如果您想要提取某些字段,则必须组合一个采用 []interface{}
之类的函数(使其成为空接口的一部分,而不是空接口代表切片),迭代它并对切片的每个元素进行类型检查,并提取任何有意义的字段,只要该元素属于您期望的类型即可:
func DoStuff(comments []interface{}) error { for _, c : = range comments { if ic, ok := c.(*github.IssueComment); ok { // Remove the deref if your slice contains values, not references // Pull out fields and do IssueComment-specific things ProcessIssueComment(ic) } else if prc, ok := c.(*github.PullRequestComment); ok { // Do PRComment-specific things ProcessPullRequestComment(prc) } else { return(fmt.Errorf("I did not want something of type %s", t)) } } return nil }
另外:游说项目所有者(如果不是您)迁移到当前版本的 go。 1.14 到 2020 年才发布,但这对于 go 版本来说已经是永恒的了。
今天带大家了解了的相关知识,希望对你有所帮助;关于Golang的技术知识我们会一点点深入介绍,欢迎大家关注golang学习网公众号,一起学习编程~
-
502 收藏
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
139 收藏
-
204 收藏
-
325 收藏
-
477 收藏
-
486 收藏
-
439 收藏
-
357 收藏
-
352 收藏
-
101 收藏
-
440 收藏
-
212 收藏
-
143 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习