By phouse512


2017-01-20 17:11:59 8 Comments

Situation:

I've learned about pointer receivers and value receivers. From what I understand: if you want to modify the object itself, you need to use a pointer receiver. I was reading more about interfaces in the go documentation and found this chunk of code:

type Sequence []int

// Methods required by sort.Interface.
func (s Sequence) Len() int {
    return len(s)
}
func (s Sequence) Less(i, j int) bool {
    return s[i] < s[j]
}
func (s Sequence) Swap(i, j int) {
    s[i], s[j] = s[j], s[i]
}
  • The Less and Len methods are using value receivers and this makes sense, because they are returning data and not modifying the Sequence state.

  • But in the Swap example, I am curious why it is still using a value receiver when it looks like it is trying to modify its state.

Question:

Is this a mistake, or is my understanding of value/pointer receivers flawed in some way?

1 comments

@Andy Schweig 2017-01-20 18:01:16

To expand a bit on @squiguy's comment, the value of a slice object is itself a reference to an underlying array, including a pointer to the element in the array at which the slice begins, the length of the slice, and the slice's capacity (the number of elements in the underlying array from the beginning of the slice to the end of the array). When you pass a slice to a function, it is the above information that is passed by value, so the slice within the function still refers to the same underlying array. This is how Swap is able to swap elements in the slice even though the slice itself is passed by value.

@phouse512 2017-01-21 03:24:34

this makes a lot of sense, I didn't understand that slices were always references to arrays. so if this is a special case, are there any other types in Go that are similar?

@Andy Schweig 2017-01-21 04:48:39

Maps behave in a similar way. If you pass a map to a function and the function makes a change to the map, the caller will see that change. Channels are also in this group (reference types), but it's probably a little more obvious that if you pass a channel to a function, both the function and the caller have a reference to the same channel.

Related Questions

Sponsored Content

13 Answered Questions

[SOLVED] What is a smart pointer and when should I use one?

39 Answered Questions

4 Answered Questions

[SOLVED] Pointers vs. values in parameters and return values

  • 2014-05-08 13:21:41
  • Zef Hemel
  • 58942 View
  • 303 Score
  • 4 Answer
  • Tags:   pointers go

21 Answered Questions

[SOLVED] Why should I use a pointer rather than the object itself?

  • 2014-03-03 11:54:16
  • gEdringer
  • 302564 View
  • 1554 Score
  • 21 Answer
  • Tags:   c++ pointers c++11

5 Answered Questions

[SOLVED] Custom errors in golang and pointer receivers

3 Answered Questions

[SOLVED] Pointer Receiver and Value Receiver on Interfaces in Golang

  • 2018-12-10 07:50:46
  • topenion
  • 477 View
  • -2 Score
  • 3 Answer
  • Tags:   go

12 Answered Questions

[SOLVED] C pointer to array/array of pointers disambiguation

1 Answered Questions

1 Answered Questions

Pointer receiver vs value receiver confusion

  • 2017-06-27 12:28:55
  • treecoder
  • 263 View
  • 2 Score
  • 1 Answer
  • Tags:   go

2 Answered Questions

[SOLVED] Pointer receiver and value receiver in Exercise:Image

  • 2016-05-25 02:54:52
  • Xu Chen
  • 185 View
  • 1 Score
  • 2 Answer
  • Tags:   go

Sponsored Content