One of Go's design goals is to be simple. But Go have both values and pointers for struct types which I think is confusing for developers to choose from, while java and javascript have a very simple rule: primitive types are always values, object types are always pointers. Why doesn't Go adopt this simple rule from java and javascript? Or is there any vital advantages of values compared to pointers?
Asked
Active
Viewed 238 times
-3
-
1Just because Java and JavaScript does something is not an argument for being sensible. C has pointers too. – Volker Jan 23 '16 at 10:26
-
1This question can yield a lot of opinionated answers – Llewellyn Collins Jan 23 '16 at 10:54
-
I've post this **[discussion](https://groups.google.com/forum/#!topic/golang-nuts/_c673wqxS60)** on golang-nuts of google group. This **[question](http://stackoverflow.com/questions/23542989/pointers-vs-values-in-parameters-and-return-values)** is what I meant to ask on stackoverflow. – Kevin Jan 24 '16 at 06:45
1 Answers
2
In Go, the flexibility allows you to optimize your program since it's by default garbage-collected. See the FAQ:
When possible, the Go compilers will allocate variables that are local to a function in that function's stack frame. However, if the compiler cannot prove that the variable is not referenced after the function returns, then the compiler must allocate the variable on the garbage-collected heap to avoid dangling pointer errors.
If a struct is returned by value, it won't be allocated on the heap. Thus, the garbage collector doesn't have to keep track of it to determine when it's safe to free the memory allocated for it.

Espresso
- 4,722
- 1
- 24
- 33