Friday, April 19, 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 in search of. 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 referred to as perform. I’ll paste the entire code right here so it’s straightforward to see.

package deal principal

import (
	"fmt"
)

kind Individual struct {
	Title string
	Age  int
}

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

func principal() {

	p := Individual{"John", 23}

	ch := make(chan Individual)

	go SendPerson(ch, p)

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

If I run this code a couple 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 referred to 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 thought of 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 positive if that is related to being buffered or unbuffered.

Can someone level me in the proper route?

Many thanks,
John.

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

When the Fundamental 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 take solely that half in

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

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

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

However as I say earlier than i’m positive 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.identify)
}

You’ll be able to 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, when you solely can simply run and down, so you possibly can block this wait the message into channel or use a sync.WaitGroup

package deal principal

import (
	"fmt"
)

kind Individual struct {
	Title string
	Age  int
	Message chan string
}

func NewPerson(identify string, age int) *Individual {
	return &Individual{
		Title: identify,
		Age: age,
		Message: make(chan string),
	}
}

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

	ch <- s
}

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

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

	ch := make(chan *Individual)
	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 way more difficult than the unique instance.

I’ve been programming on and off with go for some time now, however haven’t actually executed 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 not less than not anticipated behaviour.

I perceive, sure, once you run fmt.Print and so forth. there is no such thing as a assure that it will likely be executed as a result of there is no such thing as a order of execution of the goroutines, and in addition when your principal goroutine finishes the others will even be completed, which might 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