-
Notifications
You must be signed in to change notification settings - Fork 18k
doc: new(T) and &T{} result into different things: clarification needed #29425
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This is normal. Please read the last section in this article for details. |
well, you're not talking about composite literals as is discussed in the links that I have provided. I just want to know why do we consider new([]int) and &[]int{} equivalent while their comparison to nil result into different boolean values? |
And |
So based on what you're saying new(T) and &T{} are not equivalent for every T, am I right? |
Generally, you can think so. |
An example: package main
import (
"fmt"
)
var print = fmt.Println
func main() {
p1 := &[0]int{}
p2 := new([0]int)
print(p1, p2) // try to comment off this line to see the effect.
print (p1 == p2) // true or false
} See this for details. |
well, this example is irrelevant to what I am asking because your code may result in printing the value of false because the address of the objects may be different, but I'm concerned about the value of the objects and not their address. That's why I have considered using *arr1 and *arr2 to compare to nil and in my case, the answer is always true for *arr1 and always false for *arr2. I have compared them to nil because slices can only be compared to nil. just to make things more clarified, this is the part of the code that I am concerned about :
because if both new and composite literals do the same thing, in the case that we don't give any input for our composite literal, the value of our objects should be the same and if they don't result in the same value, I think it should be clarified in the documentation because answers in stackoverflow.com and golang.org clearly point towards the value of the objects being the same. |
Go specs says the followings:
So a However, it looks, the spec doesn't associate uninitialized values to zero values. In particular, there is a line makes people feel that uninitialized values are not zero values.
Also, the spec doesn't explicitly mention the zero values of slice type are Maybe I haven't read the spec carefully. @griesemer |
I think the documentation is fairly clear on this but I will review it to see if it can be improved. |
Thanks a lot for your answer :D . |
Well, not "on the heap". It just allocates it somewhere. The stack-vs-heap distinction doesn't exist in the spec. (You know this, but clarifying for others). |
Yes, indeed. Thanks for clarifying, @bradfitz! |
Change https://golang.org/cl/176338 mentions this issue: |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
yes
What did you do?
based on what is said on https://golang.org/doc/effective_go.html#composite_literals and https://stackoverflow.com/questions/27827871/what-the-difference-between-tnil-and-t-newt-golang
there should be no difference between new(T) and &T{} but as you can see in https://play.golang.org/p/R3yxz772TvT
the result says otherwise and these expression result into different things because one is pointing to the nil value of the type and the other one isn't.
any kind of clarification would be much appreciated.
The text was updated successfully, but these errors were encountered: