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

使用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.

  • 写回答

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 {
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

    报告相同问题?

    悬赏问题

    • ¥15 Ros2编译一个使用opencv的c++节点的时候,报了这个错误,请问怎么解决啊
    • ¥15 人脸识别相关算法 YOLO,AI等
    • ¥15 spark问题方便加V17675797079
    • ¥15 Python代码不打印的原因
    • ¥20 微软SEAL库的安装和使用,VS2022环境配置问题
    • ¥15 数学问题也不知道那种类型的问题
    • ¥15 R作图的时候,文本框重叠了怎么办?
    • ¥15 用python重建流场
    • ¥15 stm32 调试舵机pwm输出异常
    • ¥15 单片机led闪烁3秒后急速闪烁后停止