be careful with return values in go

About I was writing a program in go, where I was calling a function,which takes an existing slice, modifies it and returns it back.Then I had a print statement that prints that slice. Here, when I printed the slice, I was getting the same old values that were there before the function call. I was hitting my head to the keyboard to find the cause. Where did I screw up? Well, I did not assign the return value of the function call,which is the modified slice,to the old slice variable.In Rust or in some other languages,the compiler will yell at us if we leave the return values of a function call without assigning them. So the catch is go compiler will not give an error if we leave return values of a function call. Bottom Line So, always look for the function signature before calling the function.

May 6, 2025 - 14:59
 0
be careful with return values in go

About

I was writing a program in go, where I was calling a function,which takes an existing slice, modifies it and returns it back.Then I had a print statement that prints that slice.
Here, when I printed the slice, I was getting the same old values that were there before the function call.
I was hitting my head to the keyboard to find the cause.
Where did I screw up?
Well, I did not assign the return value of the function call,which is the modified slice,to the old slice variable.In Rust or in some other languages,the compiler will yell at us if we leave the return values of a function call without assigning them.
So the catch is go compiler will not give an error if we leave return values of a function call.

Bottom Line

So, always look for the function signature before calling the function.