Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Programmer for 8 years, in a wide variaty of languages but never touched a language that had pointers.

How much do I need to know about pointers to use and learn Go? Is Go garbage collected in the C#/Ruby sense?

I recently switched from full ORM usage to hand tuned SQL queries and loved watching the speed of my application increase 10-fold. I'm having a bit of a speed high and Go seems like the perfect language for this.

What book should I buy to learn Go from the ground up?

Edit:This seems like the perfect place to start.

http://www.golang-book.com/



If it's been 8 years, it's about time :) ...

Do yourself a favor, and learn the basics of pointers (might as well just read up on C), then move on to Go.

Before you go down the route of buying a paper book, try the interactive tutorial on the golang web site. It's pretty good. There's also a lot of good, and free, reading material on that site to help you learn.

Also, regarding pointers: They're really not that bad, but I understand a lot of people can have trouble with them.

Just realize that in languages like Ruby, and Python, you're never directly working with the computer's memory (as in RAM) - that is abstracted from you.

However, in a language like C, you are. (As an aside, you still aren't directly dealing with it, the operating system abstracts it from your program via the virtual memory system, but you can ignore that for now, and look it up some time when you see fit.) So the first thing you need to make sure is that you understand the difference between stack storage, and heap storage.

Once you understand the heap, and that it is essentially a large block of memory with sequential addresses, you can begin to understand pointers. They are just variables that contain the address of some other variable stored in memory. Languages that support pointers usually give you some syntactic sugar to make working with the "pointed to" object through the pointer easy.

Of course it can all get more complicated, but in summary, give them a shot...you'll gain a much better understanding of many things once you learn them a bit.


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


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/


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


> 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


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.


Yes, not sure for the whole PHP semantics on this, but it's a little like that.


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?


Yes, those are three special built-in objects that are reference types to their underlying stores.


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


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


>>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


> a structure that has a pointer to the underlying data by value

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


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.


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?


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.


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.


Javascript primitives (number, string, boolean) are always copied, the remaining (object, function) are passed by reference (arrays are objects). Similar to Java.


Pointers are not difficult. Take what you know about references as a starting point and go from there. In many cases, references and pointers behave very similar. When you get into the more interesting things that are possible with pointers, just remember they are merely a value that holds a memory address. If you keep that in mind, the complexity is muted a lot. Just dig into Go and see what you find.


I concur with this approach. My former languages do not have pointers either. It is refreshing to have them as an option, and they are not difficult to get used to.


I started with this. http://tour.golang.org/

Then I went through those. https://news.ycombinator.com/item?id=5365401


As others have pointed out - you probably know enough about pointers to get started from the (rather excellent) documentation.

As for learning pointers -- I've never been able to get entirely comfortable with pointers in C -- the syntax just feels unnatural to me. I much prefer either assembler (intel syntax) -- or Pascal, eg:

http://www.tutorialspoint.com/pascal/pascal_pointers.htm

Go syntax is closer to C, though. So knowing C, Go might be easier (than not knowing C) -- but it isn't a prerequisite.


>>How much do I need to know about pointers to use and learn Go?

Do you understand how they work in C? Maybe try reading a little about that. For Go, I'd recommend starting here: http://tour.golang.org/#1 (Don't miss the next arrow in the lower right-)

After running through that you should have an idea of what you don't know- which makes asking questions people can provide useful answers to much easier. :)

>>Is Go garbage collected in the C#/Ruby sense?

I'm not sure what your sense of C#/Ruby GC is, but I'd go with yes.


You already know everything you need to know about pointers. In Ruby almost everything is a pointer. What you need to learn about is 'values'.




Consider applying for YC's Fall 2026 batch! Applications are open till July 27.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: