首页 > 后端开发 > Golang > 正文

函数中输入参数的可变类型

WBOY
发布: 2024-02-09 20:54:10
转载
662 人浏览过

函数中输入参数的可变类型

php小编苹果为您介绍函数中输入参数的可变类型。在php中,函数的参数类型可以是固定的,也可以是可变的。可变类型参数,指的是函数可以接受不同类型的参数作为输入,这在处理不同场景下的数据非常有用。通过使用特殊的参数标识符,比如“...”,我们可以在函数定义中声明可变类型参数。这使得我们能够更加灵活地处理各种不同类型的数据,提高代码的复用性和可读性。无论是字符串、数字、数组或是其他类型,我们都可以轻松地将它们作为参数传递给函数,并在函数内部进行相应的处理。这种灵活的参数类型处理方式,让我们的代码更加健壮和适应性强,能够应对各种复杂的业务需求。

问题内容

我创建了一个函数来获取用户对拉取请求的最后评论。我正在使用“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 对象的切片:

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 版本来说已经是永恒的了。

以上是函数中输入参数的可变类型的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:stackoverflow.com
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!