News for hackers. Stuff that matters.
Re:Go 1.1 is released by coldtea on Monday May 13, 2013 @07:28PM (#5702092)
>How much do I need to know about pointers to use and learn Go?

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.

Re:Go 1.1 is released by kibibu on Monday May 13, 2013 @07:50PM (#5702197)
You need a little more than that, unfortunately.

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.

http://golang.org/pkg/flag/

Re:Go 1.1 is released by sergiotapia on Monday May 13, 2013 @07:34PM (#5702123)
>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.

=============================

So it's like PHP where I need to pass in `function modify(&$foo) {` to modify a variable passed by reference.

Re:Go 1.1 is released by chrisguitarguy on Monday May 13, 2013 @09:46PM (#5702609)
> 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.

0. http://php.net/manual/en/language.oop5.references.php

Re:Go 1.1 is released by dnm on Monday May 13, 2013 @09:38PM (#5702580)
It's also a performance boost when you're passing something non-trivial, to pass just the pointer, rather than have Go make a copy of whatever you're passing by value.
Re:Go 1.1 is released by coldtea on Monday May 13, 2013 @07:53PM (#5702210)
Yes, not sure for the whole PHP semantics on this, but it's a little like that.
Re:Go 1.1 is released by a_bonobo on Monday May 13, 2013 @07:43PM (#5702161)
I was under the impression that slices, maps and channels are reference types - i.e., if you pass them to a function using (foo) you'll pass "only" the reference like in Python, but that might be old information. Is that still valid?
Re:Go 1.1 is released by coldtea on Monday May 13, 2013 @07:51PM (#5702204)
Yes, those are three special built-in objects that are reference types to their underlying stores.
Re:Go 1.1 is released by voidlogic on Monday May 13, 2013 @08:56PM (#5702442)
There are no reference types in Go, everything in Go is passed by value.

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/#SliceHeader
Re:Go 1.1 is released by coldtea on Monday May 13, 2013 @09:49PM (#5702618)
>There are no reference types in Go, everything in Go is passed by value.

Yes, 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.

Re:Go 1.1 is released by voidlogic on Monday May 13, 2013 @11:31PM (#5702885)
>>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

Re:Go 1.1 is released by bishop_mandible on Tuesday May 14, 2013 @12:56AM (#5703078)
> a structure that has a pointer to the underlying data by value

Isn't this the exact definition of a reference type?

Re:Go 1.1 is released by voidlogic on Tuesday May 14, 2013 @09:53AM (#5704788)
Except that if you are passed something like a slice and you modify the length, that change is not reflected to others like it would be with a reference. See my above code example.
Re:Go 1.1 is released by cma on Tuesday May 14, 2013 @02:27AM (#5703262)
If you make a change to an integer that was in a variable and passed in by parameter, does Javascript really make the change to the caller's variable?
Re:Go 1.1 is released by eonwe on Tuesday May 14, 2013 @10:05AM (#5704875)
No. The scenario you posit would be true if JavaScript were call-by-reference.

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.
Re:Go 1.1 is released by cynwoody on Tuesday May 14, 2013 @03:13AM (#5703402)
No.

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.

Re:Go 1.1 is released by the_gipsy on Tuesday May 14, 2013 @08:23AM (#5704386)
Javascript primitives (number, string, boolean) are always copied, the remaining (object, function) are passed by reference (arrays are objects). Similar to Java.