Go 函数末尾缺少返回
我有一个Go 函数末尾缺少返回,go,rabbitmq,Go,Rabbitmq,我有一个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()
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")
}
我在函数结尾处遇到此错误缺少返回值
。
我尝试在函数末尾使用返回键盘,但出现以下错误:
not enough arguments to return
have ()
want (*amqp.Connection, *amqp.Channel, *amqp.Queue)
我所指的源视频没有任何此类问题。
我使用的是带有Go版本的Ubuntu机器go1.11.4Linux/amd64
。我正在使用Atom编辑器并安装了go lang
tools包
编辑
解决方案是我需要3个参数才能返回
return conn,ch,&q
解决了我的问题。代码的(*amqp.Connection,*amqp.Channel,*amqp.Queue)部分表示函数返回3个内容,但没有返回任何内容,这就是为什么会出现错误的原因。尝试添加
return conn, ch, q
对于应该解决问题的代码函数声明了3种返回类型,但您提供的代码没有任何语句 必须使用
return
语句指定要返回的值(在所有可能的返回路径上),例如:
或者您必须使用命名的结果参数,然后您可以有一个“裸”的return
语句(但您仍然必须有一个return
),例如:
如果您看到一个包含此函数声明和no return语句的视频,则该代码也是无效的。这不取决于Go版本或操作系统。您知道函数签名声明它将返回
(*amqp.Connection、*amqp.Channel、*amqp.Queue)
?如果您不想返回任何内容,可以发出return nil,nil,nil
(如果这是您自己的函数)。但您可能希望返回conn,ch,q
。
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")
return conn, ch, q
}
func getQueue() (conn *amqp.Connection, ch *amqp.Channel, q *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")
return
}