1

I'm a JavaScript coder who's learning Go. I'm following this tutorial: http://tour.golang.org/#52

package main

import (
    "fmt"
    "math"
)

type Vertex struct {
    X, Y float64
}

func (v *Vertex) Abs() float64 {
    return math.Sqrt(v.X*v.X + v.Y*v.Y)
}

func main() {
    v := &Vertex{3, 4}
    fmt.Println(v.Abs())
}

I read in Wikipedia and in the Go docs what pointers are, but I still can understand them. Can anyone explain them to me in JavaScript language?

alexchenco
  • 53,565
  • 76
  • 241
  • 413
  • 1
    It's going to be rather difficult since Javascript doesn't really have anything like a pointer. –  Oct 18 '14 at 06:55
  • possible duplicate of [Are there pointers in javascript?](http://stackoverflow.com/questions/17382427/are-there-pointers-in-javascript) – dizel3d Oct 18 '14 at 07:04

2 Answers2

6

They are somewhat similar to object references in JS and other languages, but not quite. Pointer are more powerful (and thus, more dangerous) than references. Consider the following JS code.

var a = {foo: true};
var b = a;
a.foo = false;
console.log(b); // Output: "Object { foo: false }"

Both a and b here are like pointer. When you do b = a you don't clone an object, you make b refer (or point if you will) to the same object as a. In Go you can do both:

type T struct { Foo bool }

a := T{Foo: true}
b := a
a.Foo = false
fmt.Println(b) // b is a copy, so it didn't change. Prints "{true}".

pa := &T{Foo: true}
pb := pa
pa.Foo = false
fmt.Println(pb) // pb points to the same struct as pa, so it prints "&{false}"

Playground

The important difference is that in JS you can't actually replace the object inside a function.

var a = {foo: true};
(function(x) { x = {foo: false} })(a);
console.log(a); // Output: "Object { foo: true }"

In Go you can do it just fine:

pa := &T{Foo: true}
func(p *T) { *p = T{Foo: false} }(pa)
fmt.Println(pa) // Output: &{false}

Playground

Another difference is that you can make pointers to not just structs, but any type, including pointers.

Ainar-G
  • 34,563
  • 13
  • 93
  • 119
  • Thanks it's a lot clear to me now. Just one thing, in the first Go example I'm still getting `true false` you missed an `*` somewhere maybe? I saw that in the tutorial. – alexchenco Oct 18 '14 at 08:47
  • @alexchenco I don't quite get you. In the first Go example I provided, the first `Println` should print "{true}", and the second, "&{false}" ("&" meaning that this is an address of a struct, a pointer, not the struct itself). It illustrates the difference between values and pointers. – Ainar-G Oct 18 '14 at 09:14
  • Sorry, I think I made I mistake somewhere. You're code is OK. Geez, I'm not sure why I find Go so difficult. Maybe because I never programmed in C. – alexchenco Oct 18 '14 at 11:25
  • 2
    @alexchenco Knowing C would help, but it's not required. Pointers are one of those things that seem hard at the beginning, but once you see that they're just memory addresses, you start to wonder, why did you ever think this was hard. – Ainar-G Oct 18 '14 at 11:57
  • @alexchenco: I'm learning Go as well and also find the pointers confusing. – user_78361084 Oct 18 '14 at 22:13
  • @llamawithabowlcut I'm curious, what language/framework did you use before. Why did you decide to learn Go? – alexchenco Oct 19 '14 at 12:37
  • @alexchenco: from Python. Switching due to gain in speed – user_78361084 Oct 19 '14 at 18:35
0

An old question but an obvious remark is that a pointer is actually the address of the variable in memory. When passing a variable by pointer what is actually passed is the memory address where this variable is.

Moshe S
  • 1
  • 2