dougou8552
2015-05-25 23:02
浏览 56
已采纳

Golang-将结构作为参数传递给函数

I have to parse some nested JSON, which translates into a Go type, like this:

type Config struct {
Mail           struct {
                   From     string
                   To       string
                   Password string
               }
Summary        struct {
                   Send     bool
                   Interval int
               }
}

Now I want to call a function for each key (Mail, Summary), I tried it like this: utils.StartNewMailer(config.Mail) The problem is, how do I construct the called function, I tried to mirror the Mail struct (and called it mailConfig), since I can't pass an arbitrary struct as an argument.
func StartNewMailer(conf mailConfig){ //..., but that doesn't work either, I get the following compiler error message: cannot use config.Mail (type struct { From string; To string; Password string }) as type utils.mailConfig in argument to utils.StartNewMailer
Do I have to pass in every single value to the called function or is there a nicer way to do this?

图片转代码服务由CSDN问答提供 功能建议

我必须解析一些嵌套的JSON,将其转换为Go类型,例如:

  type配置struct {
Mail struct {
 From string 
 To string 
 Password string 
} 
Summary struct {
 Send bool 
 Interval int 
} 
} \  n   
 
 

现在我想为每个键(邮件,摘要)调用一个函数,我这样尝试: utils.StartNewMailer(config。 Mail) 问题是,如何构造被调用的函数,我试图镜像 Mail 结构(并称为 mailConfig ),因为 我无法将任意结构作为参数传递。
func StartNewMailer(conf mailConfig){//...,但这也不起作用,我得到了以下编译器 错误消息: <代码> 不能使用config.Mail(键入struct {From string; To string; Pas 剑串})作为utils.StartNewMailer
的参数中的utils.mailConfig类型。我是否必须将每个单个值都传递给被调用的函数,或者有更好的方法吗?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题