Hacker News new | ask | show | jobs
by maxpblum 3337 days ago
I haven't used Go before; is there a common conception that you can pass by reference in Go?
3 comments

Some programmers make a liberal use of "passing by reference" (v. "passing by value") a variable when referring to passing a pointer to it (v. its value) to a function. This is common language in a C environment, where there's also no pass by ref (quick example: [0]). I'm guilty of this myself.

It's possible that this may create confusion to other people more familiar with, for example, the C++ concept of passing by reference which is passing an actual reference (v. "passing a pointer").

[0] https://www.tutorialspoint.com/cprogramming/c_function_call_...

I guess in every language with pointers, people often confuse passing the pointer as a value with passing the object as a reference.
I'm still confused and I've been working in various languages for 15 years.

If I pass a java object to a function, I'm passing a (probably) 8-byte pointer to some memory with a class tag, fields, whatever. It goes on the stack just like an 8 byte long.

In languages that support pointers more directly, I'm doing the same thing, maybe minus the class tag in the pointed-to memory. Address is in an 8 byte type, put it on the stack and access the pointed-to struct in your new frame.

Yet I've seen interview questions about whether you're "passing by reference" or "passing a reference by value" like there's some big meaningful difference and one answer is wrong.

I don't get it. Is this just one of those nerd arguments where we're debating semantics for the sake of it?

In all of those cases, you're probably passing a reference by value, semantically. ("Probably", because you didn't specify which languages, so I'm making an educated guess.)

The usual litmus test is having two objects (or ints or whatever), write a fn `swap(a, b)` where the two are swapped after the call is over. Can't do that in Java or Go or Python or C; but you can in e.g. C++ and you sorta-can in Lisp. In C++, you'd see int& (or whatever) show up in the arguments of swap().

Ok, yeah, I saw upthread about C++ and Pascal having a more formal reference type.

But in C and Go you can pass pointers to pointers, in Java and Python objects with mutable object references..

It seems like the whole article could have been "Go does not have the C++ reference type, so it's not really in the mix when calling functions".

Yep. "Reference type" is a confused and overloaded term, so I prefer to say "pass-by-reference" when I meant int& and "pass-as-reference" (or the, in my opinion confused and confusing, pass-by-object, if that's helpful to the listener).
Just to be clear, the rules of your swap challenge are that the arguments of the function have to be ints, not allowed to be int *, right?

Also you say "two objects", but if the objects were compound, e.g. arrays, then I could write swap in C.

Yes. It's about swapping the arguments. With a struct or an array or a pointer or whatever, you're not swapping the arguments themselves; but the fact that there are so many things that do something slightly similar probably illustrates the limited utility of pass-by-reference.
answer is in the title
It is not