A quick example: the standard library `flag` package lets you parse command line arguments, and returns either pointers or values. You need to know which is which. It's easy, for example, to do this:
var filename = flag.String("file", "default.txt", "file to open")
fmt.Println("Using file ", filename)
The above code looks ok at first glance, but prints the address of filename rather than its value. It can be fixed by using `flag.StringVal` instead of `flag.String`, or dereferencing `filename` using `* filename` in the Println.So I would say you need to know way less about pointers than C but you still need to know the difference.
=============================
So it's like PHP where I need to pass in `function modify(&$foo) {` to modify a variable passed by reference.
If you're thinking in terms of PHP's primitive types, then yes: that's probably the best equivalent. PHP has the whole objects always being "passed by reference" [0] thing going on too.
When you pass a slice, for example, you are passing a structure that has a pointer to the underlying data by value; you could think of it as a reference BUT its not what you think it is. What you are passing around is actually a struct that is passed by value:
type SliceHeader struct { //[1]
Data uintptr
Len int
Cap int
}
1: http://golang.org/pkg/reflect/#SliceHeaderYes, but this value is not the ACTUAL value (e.g the underlying storage).
When people (especially newcomers to pointers like the person that started this thread) speak of "pass by value" they refer to their data (the actual value).
So that Go passes _the structure_ by value is quite misleading, compared to the expectation of what "pass by value" means.
For practical purposes, I think they're better off to think of it as a reference.
No, they should learn to understand what is actually happening. Otherwise things, at best, do not make sense and, at worst, can lead to bugs when using functions like append(): http://play.golang.org/p/eTJXu6XMNJ
Isn't this the exact definition of a reference type?
JavaScript, however, is call-by-value. Primitive types (string, number, boolean) are passed to functions by copying their value, and reference types (such as any object you create) are passed by copying the value of their reference.
The reference semantics can be observed in JavaScript in some cases, such as:
var i = 0;
for (i = 0; i < 10; i++) {
setTimeout(function() {
document.write("i is now " + i);
}, 10);
}
Here the inner function gets reference of the variable i instead of its value.However, if you pass an object or an array, the called function can change a field in the object or an element in the array (the difference between those two is an optimization), and the change will be visible to the caller.
Go, however, lets you pass in an explicit pointer to an int, through which the function can change the int to which the caller pointed. Of ourse, the caller can also change the passed-in pointer if it wants, but that has no effect on the caller's variable.
Very little. Just what pass by value and pass by reference mean in the context of pointers.
The very basic you need to know is: if a function argument is defined as (*foo), you are changing the item you pass to the function (like in Javascript or Java), whereas if it's defined as (foo), you're changing a copy of it and the original item remains unaffected.
Go has pointers, but no pointer arithmetic (in the base language), and it also has GC, so most of the problems people have with pointers in C are not there.