Live data from Hacker News

There is no pass by reference in Go

dave.cheney.net

1–10 of 70 posts

Re: There is no pass by reference in Go

#2
Interestingly, the term "reference type" has been removed from the Go spec in 2013: https://github.com/golang/go/commit/b34f0551387fcf043d65cd7d...

Yet some semi-official sources (like The Go Programming Language book) do refer to channels as "reference types".

TBH, I find cheney's post not fully useful without explaining what does happen behind the scenes in maps so that passing them by value doesn't incur large copies.

Re: There is no pass by reference in Go

#4
post #2

Interestingly, the term "reference type" has been removed from the Go spec in 2013: https://github.com/golang/go/commit/b34f0551387fcf043d65cd7d... Yet some semi-official sources (like The Go Programming Language book) do refer to channels as "reference types". TBH, I find cheney's post not fully useful without explaining what does happen behind the scenes in maps so that passing them by value doesn't incur large cop…

A map is a tiny struct containing a pointer to the bucket storage. It's always cheap to pass a map value.

Re: There is no pass by reference in Go

#8
post #4
post #2

Interestingly, the term "reference type" has been removed from the Go spec in 2013: https://github.com/golang/go/commit/b34f0551387fcf043d65cd7d... Yet some semi-official sources (like The Go Programming Language book) do refer to channels as "reference types". TBH, I find cheney's post not fully useful without explaining what does happen behind the scenes in maps so that passing them by value doesn't incur large cop…

A map is a tiny struct containing a pointer to the bucket storage. It's always cheap to pass a map value.

Right.

I'm saying the linked blog post can be improved significantly by spending a paragraph on explaining this. Otherwise it just sounds like magic.

Re: There is no pass by reference in Go

#9
It's a question of semantics and the poorly chosen word "reference".

The roots come from C, where pretty much every book on the subject talked about the difference between passing a huge, expensive value to a function vs passing a cheap pointer "reference" to it.

And while the semantic purists will scream and holler, the fact remains that, colloquially, a pointer is a reference to something, not the something itself. And so this annoying debate rears its ugly head a few times a year and makes it into HN or Slashdot or whatever.

Re: There is no pass by reference in Go

#10
An extended explanation missing from Dave Cheney's answer is that the "reference" etymology has at least 2 meanings. This is why his explanation is virtually the same as the one 20 years ago in the C Language FAQ[1] compiled by Steve Summit.

- "reference" definition 1: an alias that cannot be null which is what a "reference type" is in Pascal and C++. This might be thought of as a stricter "computer-science" definition. The C and Go languages don't have this type of "reference".

- "reference" definition 2: a pointer variable that lets programmers change the "thing pointed to" instead of the pointer variable itself to avoid copying unnecessary bytes. This is the colloquial usage. It also doesn't help that we call the star operator the "de-REFERENCING" operator[2] instead of the "de-POINTERIZING" operator. (What are we "derefencing" if it's not a "reference"?!?)

Cheney is talking about definition 1.

[1] http://www.di-srv.unisa.it/~vitsca/LAB/C-faq.html excerpt:

  4.11:	Does C even have "pass by reference"?

  A:	Not really.  Strictly speaking, C always uses pass by value.
	You can simulate pass by reference yourself, by defining
	functions which accept pointers and then using the & operator
	when calling, and the compiler will essentially simulate it for
	you when you pass an array to a function (by passing a pointer
	instead, see question 6.4 et al.).  However, C has nothing truly
	equivalent to formal pass by reference or C++ reference
	parameters.  (On the other hand, function-like preprocessor
	macros can provide a form of "pass by name".)
[2] https://en.wikipedia.org/wiki/Dereference_operator
Post reply on HN