I have a getQueue()
function to create a connection and channel for my Go client to a RabbitMQ instance. I have this code for the aforementioned function:
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")
}
I am getting this error Missing return at end of function
for the same. I tried using return keyboard at the end of the function, but then I get this error:
not enough arguments to return
have ()
want (*amqp.Connection, *amqp.Channel, *amqp.Queue)
The source video I was referring did not have any such issues. I am using Ubuntu machine with Go version go1.11.4 linux/amd64
. I am using Atom editor with go-lang
tools package installed.
EDIT The solution is that I needed 3 parameters to return return conn,ch,&q
solved my issue.
the (*amqp.Connection, *amqp.Channel, *amqp.Queue) part of your code says your function returns 3 things, but you don't return anything that is why you get the error. Try adding
return conn, ch, q
to your code that should solve the problem
Your function declares 3 return types, yet the code you presented has no return
statements.
You must either use the return
statement to specify what values you want to return (on all possible return paths), e.g.:
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
}
Or you must use named result parameters, and then you can have a "naked" return
statement (but you still must have a return
), e.g.:
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
}
If you saw a video with this function declaration and no return statement, that code is also invalid. This does not depend on Go version or OS.