通过通道值中的指针分配给局部变量

Code Here: http://play.golang.org/p/WjpgN_0AaP

On lines 45,46 and 47 there are three different ways to pull the value off the message broker.

//var mb MessageBroker = *<-in
mb := *<-in
//mb := <-in    

All three of these have the exact same result. What is the significance of choosing one way over the other? Also, I'm confused as to why the asterix seemingly makes no difference.

Looking at the declaration of your function:

func (c *MyGui) Receive(in <-chan *MessageBroker) {

We can see that <-in will give you a value of type *MessageBroker, a pointer to a MessageBroker struct.

Putting an asterisk before a pointer value will dereference it (see Go spec)

That means *<-in will dereference the *MessageBroker pointer and give you a value of type MessageBroker.

So, looking at your examples again:

//var mb MessageBroker = *<-in // mb is explicitly declared as a MessageBroker
mb := *<-in // mb is implicitly declared as a MessageBroker using short variable declaration
//mb := <-in // mb is implicitly declared as a *MessageBroker pointer.

So, the two first options are identical, but the third will have mb being a pointer to the struct instead. In your particular case it doesn't really matter if it is a pointer or not; you are just printing the Message.

However, the difference would be if you were to change the message:

mb.Message := "New message"

If mb was of type MessageBroker, a struct value, then the change would just be for the local mb variable, but would have no effect outside of the Receive function.

However, if mb is of type *MessageBroker, you would change the same instance of the object as you received through the channel.