Skip to content

doc: new(T) and &T{} result into different things: clarification needed #29425

Closed
@pooriaPoorsarvi

Description

@pooriaPoorsarvi

What version of Go are you using (go version)?

$ go version 1.11.1.

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

package main

import (
	"fmt"
)

func main() {
	arr1 := new([]int)
	fmt.Println(*arr1, len(*arr1))
	fmt.Println(*arr1 == nil)
	fmt.Println(arr1 == nil)
	fmt.Println()
	arr2 :=  &[]int {}
	fmt.Println(*arr2, len(*arr2))
	fmt.Println(*arr2 == nil)
	fmt.Println(arr2 == nil)
	fmt.Println()
}
/*
output : 
[] 0
true
false

[] 0
false
false

*/

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions