Answer the question
In order to leave comments, you need to log in
How to cast < -chan amqp.Delivery to < -chan interface{}?
For an example sketched such code. The purpose of the function is to return a channel with an interface to implement the channel's abstract type.
package main
import (
"fmt"
"github.com/streadway/amqp"
)
type (
IConsumer interface {
Consume() (<-chan interface{}, error)
}
AmqpConsumer struct {
conn *amqp.Connection
topic string
channel *amqp.Channel
}
)
func (pr *AmqpConsumer) Consume() (<-chan interface{}, error) {
var msgs <-chan interface{}
ch, err := pr.conn.Channel()
if err != nil {
return msgs, err
}
defer ch.Close()
amqp_msgs, err := ch.Consume(
pr.topic, // queue
"", // consumer
true, // auto-ack
false, // exclusive
false, // no-local
false, // no-wait
nil, // args
)
if err != nil {
return msgs, err
}
return amqp_msgs, nil
}
func main() {
fmt.Println("Hello, playground")
}
./prog.go:41:2: cannot use amqp_msgs (type <-chan amqp.Delivery) as type <-chan interface {} in return argument
Answer the question
In order to leave comments, you need to log in
Casting channels seems to be impossible in go. You will have to create an intermediate channel. That's pretty much how I see it. But most likely it does not work as it should
https://play.golang.org/p/i8-69C6RDrG
package main
import (
"fmt"
"github.com/streadway/amqp"
)
type (
IConsumer interface {
Consume() (<-chan interface{}, error)
}
AmqpConsumer struct {
conn *amqp.Connection
topic string
channel *amqp.Channel
}
)
func (pr *AmqpConsumer) Consume() (<-chan interface{}, error) {
var msgs <-chan interface{}
ch, err := pr.conn.Channel()
if err != nil {
return msgs, err
}
defer ch.Close()
amqp_msgs, err := ch.Consume(
pr.topic, // queue
"", // consumer
true, // auto-ack
false, // exclusive
false, // no-local
false, // no-wait
nil, // args
)
if err != nil {
return msgs, err
}
var castCh chan interface{}
go func() {
for msg := range amqp_msgs {
castCh <- msg
}
}()
return castCh, nil
}
func main() {
fmt.Println("Hello, playground")
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question