3 ms·
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
by voidlogic 13y ago
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 http://golang.org/pkg/reflect/#SliceHeader
- coldtea 13y ago>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.
- voidlogic 13y ago>>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 http://play.golang.org/p/eTJXu6XMNJ
- bishop_mandible 13y ago> a structure that has a pointer to the underlying data by value Isn't this the exact definition of a reference type?
- voidlogic 13y agoExcept 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.