Non-method funcs

273 views
Skip to first unread message

nc

unread,
Mar 27, 2023, 10:05:16 AM3/27/23
to golang-nuts
Hello,

I have found that I have a tendency to try to code non-method funcs in a pure way (functionally speaking - so that the caller will not modify any of its params). I know it is  possible to pass a pointer (or slice or map - is that all of them?) as a function param and then modify it in the called func like is done in https://gobyexample.com/pointers in the zeroptr func, but I have a strong preference to try to do this as rarely as possible and to never modify func params if I can get away with it. However I see some Go code written by others that defintely doesn't agree with this idea and I wonder if perhaps I am misguided. Perhaps it would make sense in another language but doesn't have a place in Go. I am not really sure.

I just wanted to know if anybody had any well-formed opinions on this based on tested experience with Go. Thanks for your help.

- nc

Henry

unread,
Mar 27, 2023, 10:17:58 AM3/27/23
to golang-nuts
Hi,

Try to write readable code. If your function modifies any of its parameters, make it obvious. Name the function properly and/or write a comment. If the function does not modify its parameters, make it obvious too. There is no hard rule on this one. Sometimes it makes sense to use pure functions and sometimes it doesn't.

Henry

nc

unread,
Mar 29, 2023, 8:20:09 AM3/29/23
to golang-nuts
Just adding a quick note: It turns out this topic has been discussed before. I thought I tried to search but perhaps I had been using the wrong keyword. If you search for "pure" or "purity" you will see a lot of stuff. I am hoping to review it all soon and see if I can come to any conclusions based on the previous discussion. Thanks.
Reply all
Reply to author
Forward
0 new messages