Thursday, April 25, 2024
HomeGolangA query about channels - Getting Assist

A query about channels – Getting Assist


Hello,

I hope this isn’t a boneheaded query. I’ve searched on this discussion board however haven’t seen fairly what I’m on the lookout for. This publish about attainable race circumstances appears a possible match.

I’ve been channels on golangdocs.com, sending customized information through channels. I’ve made one tiny change, including a Printf assertion to the known as operate. I’ll paste the entire code right here so it’s straightforward to see.

package deal predominant

import (
	"fmt"
)

kind Particular person struct {
	Title string
	Age  int
}

func SendPerson(ch chan Particular person, p Particular person) {
	ch <- p
	fmt.Printf("SendPerson %vn", p)
}

func predominant() {

	p := Particular person{"John", 23}

	ch := make(chan Particular person)

	go SendPerson(ch, p)

	title := (<-ch).Title
	fmt.Println(title)
}

If I run this code a number of occasions I often see simply John. Generally I see:

SendPerson {John 23}
John

I don’t fairly perceive why. I assumed this was an error on my half as I ought to have put the Printf assertion within the known as operate earlier than the channel dealing with half as a result of I needed to see what I used to be receiving (that was the aim of including the Printf within the first place). However then I considered it a bit extra. Why do I see this typically? I might count on to see it all the time or by no means, however not typically seemingly at random. I’m not certain if that is linked to being buffered or unbuffered.

Can anyone level me in the fitting course?

Many thanks,
John.

Hello @MrJohn
The SendMessage operate (and goroutine) ends earlier than it could print the message.
Attempt to print after which ship the message to the channel.

When the Important operate finalize, it kills all of the others goroutines.
It’s like a race situation.

Hello @GonzaSaya,

Thanks, I guessed it was a race situation. However I didn’t fairly perceive why.

Thanks once more.
John.

I’m actually don’t perceive your query absolutely. It print “John” becase you take solely that half in

title := (<-ch).Title
fmt.Println(title)

So if you’d like all of the construction within the channel, simply

perso := <-ch
fmt.Println(perso)

However as I say earlier than i’m certain if i perceive your query. Let me know…

Yamil

gorountine runtime is unsure.
attempt to this, it woundn’t till block that one msgs be ship to channle
for p:=vary ch{
fmt.Println(p.title)
}

You may change your method to ship message into channel to, since you assure that your message are logged, in fact in case your program operating in background, if you happen to solely can simply run and down, so you’ll be able to block this wait the message into channel or use a sync.WaitGroup

package deal predominant

import (
	"fmt"
)

kind Particular person struct {
	Title string
	Age  int
	Message chan string
}

func NewPerson(title string, age int) *Particular person {
	return &Particular person{
		Title: title,
		Age: age,
		Message: make(chan string),
	}
}

func (s *Particular person) Ship(ch chan *Particular person) {
	s.Message <- "The message was despatched"

	ch <- s
}

func (s *Particular person) ReadMessage() {
	go func(){
		for m := vary s.Message {
			fmt.Println(m)
		}
	}()
}

func predominant() {
	p := NewPerson("John", 23)
	p. ReadMessage()

	ch := make(chan *Particular person)
	go p.Ship(ch)

	fmt.Println((<-ch).Title)
}

https://play.golang.com/p/D4akv-iVII__i

gorountine runtime is unsure.

Aha. I believe that solutions it.

Many thanks for the code. It does appear much more sophisticated than the unique instance.

I’ve been programming on and off with go for some time now, however haven’t actually accomplished something with channels. I used to be a bit shocked at just about my first have a look at them that I used to be getting a race situation of some type, or not less than not anticipated behaviour.

I perceive, sure, while you run fmt.Print and so forth. there is no such thing as a assure that will probably be executed as a result of there is no such thing as a order of execution of the goroutines, and likewise when your predominant goroutine finishes the others can even be completed, which may occur in case your print by no means seems

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments