tag:blogger.com,1999:blog-8110657754519829885.post8060742057817931346..comments2023-07-10T01:54:55.230-07:00Comments on GoLang Tutorials: Channels in Go - range and selectSathish VJhttp://www.blogger.com/profile/15718521031646368740noreply@blogger.comBlogger13125tag:blogger.com,1999:blog-8110657754519829885.post-40179384671630676652016-04-10T12:19:15.904-07:002016-04-10T12:19:15.904-07:00That works, but the semantics have changed. Your p...That works, but the semantics have changed. Your program makes the packer alternate between strawberry and chocolate evenly.<br /><br />When using the `select` statement, the packer can receive strawberry or chocolate cakes in any order.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8110657754519829885.post-48312495052789764182014-07-12T17:51:34.533-07:002014-07-12T17:51:34.533-07:00Please remove the thread sleep and update as Andre...Please remove the thread sleep and update as Andrew recommended. This page turns up at the top of google search result and this code has errors that should not be using for teaching channels.<br /><br /> Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8110657754519829885.post-76485702055501023292014-04-17T12:16:34.903-07:002014-04-17T12:16:34.903-07:00The code in the second example works almost as-is ...The code in the second example works almost as-is (after changing the sleep at the end to "time.Sleep( 2 * time.Second )". <br /><br />However, it's making the same number of both types of cake. If you try to change the code to make (eg) 3 strawberry and 8 chocolate cakes, the program never terminates.<br /><br />Ernest's EOF-like token (send a cake named "END" when Jonnoreply@blogger.comtag:blogger.com,1999:blog-8110657754519829885.post-15113291618137258832013-09-19T05:44:50.474-07:002013-09-19T05:44:50.474-07:00When using buffered channels, closing is not the r...When using buffered channels, closing is not the right solution ; every one in the chain must be drained first. Recently, I solved this problem by passing an EOF-like token. Each channel in my chain detect this and passes this to the next. The consumer and the end of the chain now knows when to terminate.Ernesthttp://ernestmicklei.comnoreply@blogger.comtag:blogger.com,1999:blog-8110657754519829885.post-6861407582390788182013-08-04T21:40:23.128-07:002013-08-04T21:40:23.128-07:00Thanks a lot for these tutorials. This is hands do...Thanks a lot for these tutorials. This is hands down the best available resource for newcomers to the language. Well done!Jashhttps://www.blogger.com/profile/16381614310218706491noreply@blogger.comtag:blogger.com,1999:blog-8110657754519829885.post-53129226901682210342013-08-04T21:39:09.658-07:002013-08-04T21:39:09.658-07:00@Andrew's solution is the correct implementati...@Andrew's solution is the correct implementationJashhttps://www.blogger.com/profile/16381614310218706491noreply@blogger.comtag:blogger.com,1999:blog-8110657754519829885.post-67191289939457299102013-06-23T03:13:37.179-07:002013-06-23T03:13:37.179-07:00This comment has been removed by the author.Anonymoushttps://www.blogger.com/profile/13559122266720112339noreply@blogger.comtag:blogger.com,1999:blog-8110657754519829885.post-83014309758233865302013-01-25T06:06:58.395-08:002013-01-25T06:06:58.395-08:00There is no such coupling between go routines and ...There is no such coupling between go routines and channels. If you have a reference to a channel, it is open, and it's type allows it, you can send or receive on it at whim from wherever.<br /><br />I mention type because you can limit how a channel can be used with the type system, but it has nothing to do with go routines per se.<br /><br />Jessehttps://www.blogger.com/profile/18324998635607736552noreply@blogger.comtag:blogger.com,1999:blog-8110657754519829885.post-74323095889847983012012-08-29T19:32:59.161-07:002012-08-29T19:32:59.161-07:00I found this not to be true, and I am recording it...I found this not to be true, and I am recording it here. What happens is that the range statement never finishes, but the program exits once the main thread exits, so it does not matter that the range statement never finishes.<br /><br />The garbage collector cannot collect the channel since there is still a goroutine waiting on it, and it cannot know that none of the references held to the Andrewhttp://www.something.comnoreply@blogger.comtag:blogger.com,1999:blog-8110657754519829885.post-89813246431036209862012-05-31T08:36:19.557-07:002012-05-31T08:36:19.557-07:00My Solution:
func receiveCakeAndPack(strbry_cs ch...My Solution:<br /><br />func receiveCakeAndPack(strbry_cs chan string, choco_cs chan string) { <br /> strbry_open, choco_open := true, true <br /> for cakeName := ""; strbry_open || choco_open; {<br /> fmt.Println("Waiting for a new cake ...")<br /> if cakeName, strbry_open = <- strbry_cs; strbry_open {<br /> fmt.Println("Received from Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8110657754519829885.post-17193406214705640272012-05-10T05:45:46.265-07:002012-05-10T05:45:46.265-07:00Tested with and without close - both works. Does t...Tested with and without close - both works. Does the garbage collector closes channel automatically?Gregorynoreply@blogger.comtag:blogger.com,1999:blog-8110657754519829885.post-64924149008844239212012-05-10T02:13:33.095-07:002012-05-10T02:13:33.095-07:00Great stuff!
If I'm not mistaken, doesn't...Great stuff!<br /><br />If I'm not mistaken, doesn't your first example forget to close the channel, in order to trigger the range statement?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8110657754519829885.post-68782885909392140072012-05-04T09:05:58.034-07:002012-05-04T09:05:58.034-07:00It looks like a channel can only be used by one go...It looks like a channel can only be used by one goroutine to send. Can a goroutine leave the channel open for another goroutine to send?i'm an idiot!https://www.blogger.com/profile/12142477461519516966noreply@blogger.com