Saturday, April 20, 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 put up about doable race situations appears a probable match.

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

bundle principal

import (
	"fmt"
)

kind Particular person struct {
	Identify string
	Age  int
}

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

func principal() {

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

	ch := make(chan Particular person)

	go SendPerson(ch, p)

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

If I run this code a couple of instances I normally see simply John. Typically I see:

SendPerson {John 23}
John

I don’t fairly perceive why. I believed this was an error on my half as I ought to have put the Printf assertion within the known as perform earlier than the channel dealing with half as a result of I wished 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’d 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 any individual level me in the suitable path?

Many thanks,
John.

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

When the Primary perform 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 totally. It print “John” becase you’re taking solely that half in

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

So if you need 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 possibly can 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 may block this wait the message into channel or use a sync.WaitGroup

bundle principal

import (
	"fmt"
)

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

func NewPerson(title string, age int) *Particular person {
	return &Particular person{
		Identify: 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 principal() {
	p := NewPerson("John", 23)
	p. ReadMessage()

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

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

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 way more difficult than the unique instance.

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

I perceive, sure, whenever you run fmt.Print and so forth. there isn’t a assure that it will likely be executed as a result of there isn’t a order of execution of the goroutines, and in addition when your principal goroutine finishes the others may also 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