douhan4812
2017-06-06 21:52
浏览 124
已采纳

使用gomail.v2创建新的* Dialer

I'm using gomail.v2 to send emails, and my code works fine. After composing a message msg I can just run

import ("gopkg.in/gomail.v2")

  ...
  d := gomail.NewDialer("smtp.example.com", 25, "username", "password")
  return d.DialAndSend(msg)
}

Of course I'd like to generalize this past a particular username and password and separate it into its own function, so I stubbed it out:

import ("gopkg.in/gomail.v2")

  ...
  d := MyDialer()
  return d.DialAndSend(msg)
}

func MyDialer() *Dialer {
  return gomail.NewDialer("smtp.example.com", 25, "username", "password")
}

But go croaks, complaining that it doesn't know about Dialer.

.\email.go:42: undefined: Dialer

Why is this? I'm using the same return type as NewDialer, which doesn't cause any problems.

func NewDialer(host string, port int, username, password string) *Dialer

What am I missing? I ran

go get -u gopkg.in/gomail.v2

to make sure I wasn't somehow running an out of date version of the package, but no luck there.

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

我正在使用 gomail.v2 发送电子邮件,我的代码运行正常。 编写消息 msg 后,我可以运行

  import(“ gopkg.in/gomail.v2")

 ... \  nd:= gomail.NewDialer(“ smtp.example.com”,25,“用户名”,“密码”)
返回d.DialAndSend(msg)
} 
   
 \  n 

当然,我想通过一个特定的用户名和密码对此进行概括,并将其分成自己的功能,因此我将其删除:

  import(“  gopkg.in/gomail.v2")

 ... 
d:= MyDialer()
返回d.DialAndSend(msg)
} 
 
func MyDialer()*拨号器{
返回gomail。  NewDialer(“ smtp.example.com”,25,“用户名”,“密码”)
} 
   
 
 

但是发牢骚,抱怨它没有

。\ email.go:42:未定义:Dialer

为什么 这个? 我使用的返回类型与 NewDialer 相同,这不会引起任何问题。

  func NewDialer(主机字符串,端口int,用户名 ,密码字符串)*拨号器
   
 
 

我缺少什么? 我跑了

去得到-u gopkg.in/gomail.v2

以确保我 没有以某种方式运行该软件包的过时版本,但是在那里没有运气。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • drsc10888 2017-06-06 21:55
    已采纳

    *Dialer and *gomail.Dialer are 2 different types. The specific error is because you don't have a Dialer type defined in your package, but you need to match the types in the signature, not just the name. Since gomail.NewDialer returns a *gomail.Dialer, use:

    func MyDialer() *gomail.Dialer {
    
    点赞 评论

相关推荐 更多相似问题