登录
首页 >  Golang >  Go问答

缺少函数结尾的返回语句

来源:stackoverflow

时间:2024-03-26 19:00:31 468浏览 收藏

在 Go 语言中,`getqueue()` 函数缺少返回语句。该函数旨在返回三个值:`*amqp.connection`、`*amqp.channel` 和 `*amqp.queue`,但当前代码中没有返回任何值。此错误消息提示在函数末尾需要使用 `return` 关键字并提供所需的三个参数才能成功执行返回操作。

问题内容

我有一个 getqueue() 函数来为我的 go 客户端到 rabbitmq 实例创建连接和通道。我有上述函数的代码:

func getqueue() (*amqp.connection, *amqp.channel, *amqp.queue) {
    conn, err := amqp.dial("amqp://ayman@localhost:5672")
    fallonerror(err, "fail to connect")
    ch, err := conn.channel()
    fallonerror(err, "fail to open channel")
    q, err := ch.queuedeclare("hello",
        false, //durable
        false, //autodelete
        false, //exclusive
        false, //nowait
        nil)   //args
    fallonerror(err, "fail to delare a queue")

}

我收到此错误 missing return at end of function 相同。 我尝试在函数末尾使用返回键盘,但随后出现此错误:

not enough arguments to return
have ()
want (*amqp.Connection, *amqp.Channel, *amqp.Queue)

我提到的源视频没有任何此类问题。 我使用的是 ubuntu 机器,go 版本为 go1.11.4 linux/amd64。我正在使用安装了 go-lang 工具包的 atom 编辑器。

编辑 解决方案是我需要3个参数来返回 return conn,ch,&q 解决了我的问题。


解决方案


代码的 (*amqp.connection, *amqp.channel, *amqp.queue) 部分表示您的函数返回 3 个内容,但您没有返回任何内容,这就是您收到错误的原因。尝试添加

return conn, ch, q

您的代码应该可以解决问题

文中关于的知识介绍,希望对你的学习有所帮助!若是受益匪浅,那就动动鼠标收藏这篇《缺少函数结尾的返回语句》文章吧,也可关注golang学习网公众号了解相关技术文章。

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>