{"id":3280,"date":"2020-07-30T13:29:40","date_gmt":"2020-07-30T07:59:40","guid":{"rendered":"https:\/\/golangbyexamples.com\/?p=3280"},"modified":"2020-07-30T13:33:21","modified_gmt":"2020-07-30T08:03:21","slug":"receive-multiple-return-value-goroutine-golang","status":"publish","type":"post","link":"https:\/\/golangbyexamples.com\/receive-multiple-return-value-goroutine-golang\/","title":{"rendered":"Receive or fetch multiple return values from a goroutine in Go(Golang)"},"content":{"rendered":"\n
Channels can be used to fetch return value from a goroutine. Channels provide synchronization and communication between goroutines. You can send the return value to a channel in the goroutine and then collect that value in the main function. <\/p>\n\n\n\n
To receive multiple values we can create a custom struct type with fields for both return values, then create a channel of that type.<\/p>\n\n\n\n
Let’s see a program<\/p>\n\n\n\n
package main\n\nimport (\n\t\"fmt\"\n\t\"time\"\n)\n\ntype result struct {\n\tsumValue int\n\tmultiplyValue int\n}\n\nfunc main() {\n\tresultChan := make(chan result, 1)\n\tsumAndMultiply(2, 3, resultChan)\n\n\tres := <-resultChan\n\tfmt.Printf(\"Sum Value: %d\\n\", res.sumValue)\n\tfmt.Printf(\"Multiply Value: %d\\n\", res.multiplyValue)\n\tclose(resultChan)\n\n}\n\nfunc sumAndMultiply(a, b int, resultChan chan result) {\n\tsumValue := a + b\n\tmultiplyValue := a * b\n\tres := result{sumValue: sumValue, multiplyValue: multiplyValue}\n\ttime.Sleep(time.Second * 2)\n\tresultChan <- res\n\treturn\n}<\/code><\/pre>\n\n\n\nOutput<\/strong><\/p>\n\n\n\nSum Value: 5\nMultiply Value: 6<\/code><\/pre>\n\n\n\nIn the above program we created a struct named result<\/strong> which has two fields<\/p>\n\n\n\n- sumValue<\/li><\/ul>\n\n\n\n
- multiplyValue<\/li><\/ul>\n\n\n\n
We then created a variable resultChan<\/strong> which is a channel of length 1 holding value of result<\/strong> struct type. We passed this channel to the sumAndMultiply<\/strong> function. The sumAndMultiply<\/strong> function pushes the resultant struct to the resultChan<\/strong><\/p>\n\n\n\nres := result{sumValue: sumValue, multiplyValue: multiplyValue}\nresultChan <- res<\/code><\/pre>\n\n\n\nThen in the main function we are waiting on the channel to collect the result<\/p>\n\n\n\n
res := <-resultChan<\/code><\/pre>\n\n\n\nThis line will wait until a value is pushed to the resultChan<\/strong> channel.<\/p>\n","protected":false},"excerpt":{"rendered":"Channels can be used to fetch return value from a goroutine. Channels provide synchronization and communication between goroutines. You can send the return value to a channel in the goroutine and then…<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[1],"tags":[3,4],"class_list":["post-3280","post","type-post","status-publish","format-standard","hentry","category-tech","tag-go","tag-golang"],"yoast_head":"\n
Receive or fetch multiple return values from a goroutine in Go(Golang) - Welcome To Golang By Example<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\n\t\n\t\n