Unable to return multiple values only the first or the last

64 views
Skip to first unread message

Bishwajit Samanta

unread,
Nov 12, 2021, 12:20:10 PM11/12/21
to golang-nuts
HI, need help in understanding what i am making mistake of not able to return all the values. Below is the sample code from Playground.
https://play.golang.org/p/EMN830T0Lvr

Roman Kuprov

unread,
Nov 12, 2021, 12:35:46 PM11/12/21
to golang-nuts
You're returning right after ranging through the first value, thus ending/exiting the function. 

Brian Candler

unread,
Nov 12, 2021, 12:39:04 PM11/12/21
to golang-nuts
Your function is declared to return only a single string, and the "return" statement returns immediately from the function with that single string, so this is what you've coded it to do.

If you want to return multiple values, then (1) you'll have to declare the function to return a slice of strings, and (2) you'll need to build up that slice, and return it at the end.

Reply all
Reply to author
Forward
0 new messages