Re: [go-nuts] Need explain on error inteface work?

2016-09-16 Thread Giang Tran
got it, thank you.

On Saturday, September 17, 2016 at 12:14:34 AM UTC+7, Ian Lance Taylor 
wrote:
>
> On Fri, Sep 16, 2016 at 8:24 AM, Giang Tran <sec...@gmail.com 
> > wrote: 
> > 
> > I have a small test like this 
> > 
> > package main 
> > 
> > type MError struct { 
> > 
> > } 
> > 
> > func (m *MError) Error() string { 
> >  return "MError" 
> > } 
> > 
> > func NewMError() *MError { 
> >  return nil 
> > } 
> > 
> > func main() { 
> >  var e error 
> >  e = NewMError() 
> >  println(e.Error()) 
> > } 
> > 
> > 
> > 
> > I know that interface actually combine like (Type, value), if I change 
> > 
> > 
> > func (m *MError) Error() string { 
> > 
> > 
> >  to 
> > 
> > 
> > func (m MError) Error() string { 
> > 
> > 
> > would lead to "panic: value method main.MError.Error called using nil 
> > *MError pointer", 
> > 
> > why we have this different behave? 
>
> When you have a pointer, and call a value method, what that means is 
> that the value is copied out of the pointer and then the method is 
> invoked.  That is, when you write e.Error(), where e is type *MError 
> and Error is a value method, the actual code is something like 
> tmp := *e 
> tmp.Error() 
> But in this case e is nil, so copying out the value fails, and you get a 
> panic. 
>
> Ian 
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Need explain on error inteface work?

2016-09-16 Thread Giang Tran
Hello

I have a small test like this

package main

type MError struct {

}

func (m *MError) Error() string {
 return "MError"
}

func NewMError() *MError {
 return nil
}

func main() {
 var e error
 e = NewMError()
 println(e.Error())
}



I know that interface actually combine like (Type, value), if I change


func (m *MError) Error() string {


 to 


func (m MError) Error() string {


would lead to "panic: value method main.MError.Error called using nil *MError 
pointer", 

why we have this different behave?


-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Re: Golang Rabbit MQ Fanout Exchange Multiple Consumers

2016-07-04 Thread Giang Tran
it's because you use single queue for both of consumer, in fanout exchange, 
message is copy to all the queue bind to this exchange.
Inorder to has 2 copy, you must have to use 2 queue(use different queue 
name).

On Monday, July 4, 2016 at 10:09:03 PM UTC+7, Rakesh Goyal wrote:
>
>
>
> down votefavorite 
> 
>
> I am publishing messages in fanout exchange from Java application. I am 
> able to receive message in multiple consumer in Java. I have 2 consumers in 
> golang app but only one of the consumer (alternatively ) is receiving the 
> message (Not both of them for a published message).
>
> func HandleMessageFanout1(){
>
> conn := system.EltropyAppContext.RabbitMQConn
>
> channel, err := conn.Channel()
>
> if(err!=nil){
> log.Println(err)
> }
> //forever := make(chan bool)
>
>
>
> deliveries,err := channel.Consume(
> "example.queue", //queue
> "qw",
> true,
> false,
> false,
> false,
> nil)
>
> if(err!=nil){
> log.Println(err)
> }
>
> go func() {
>
> for d := range deliveries {
> log.Printf("Message recived in fanout 1")
> log.Printf("Received a message: %s", d.Body)
> }
> }()
>
> //<-forever
>
> }
>
> //2nd Consumer
>
> package consumer
>
> import (
> "github.com/eltropy/shehnai/backend/golang/common-packages/system"
> log "github.com/Sirupsen/logrus"
> )
>
> func HandleMessageFanout2() {
>
> conn := system.EltropyAppContext.RabbitMQConn
>
> channel, err := conn.Channel()
>
> if (err!=nil) {
> log.Println(err)
> }
>
> //forever := make(chan bool)
>
> deliveries, err := channel.Consume(
> "example.queue", //queue
> "q2",
> true,
> false,
> false,
> false,
> nil)
>
> if (err!=nil) {
> log.Println(err)
> }
>
> go func() {
> for d := range deliveries {
> log.Printf("Message recived in fanout 2")
> log.Printf("Received a message: %s", d.Body)
> }
> }()
>
> //<-forever
>
> }
>
> I am using https://github.com/streadway/amqp library for rabbit mq.
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.