DEV Community

Cover image for How is new() different in Go?
BhanuReddy
BhanuReddy

Posted on

How is new() different in Go?

what is new()?

new() is a builtin function, used to create zero values(doesn't initializes memory) and returns pointer. new() can be used to declare any data type. In other words it returns a pointer to a newly allocated zero value of type T.

what is make()?

make() is a builtin function, used to create non zero value and it does not return pointer. It is used to create only map, slice and channels data types. Using make() we can also define length, capacity for slice, initial capacity for map and buffer length for channels.

Image description

Can we create map, slices and channels with new function?

what is the distinction with these 3 data types? Because these three data types under the covers refers to data structures which has to be initialized before use. For maps, slices and channels make initializes the internal data structure and prepares the value.

We can create zero valued types, but even without using new() function same functionality can be achieved below are the examples

new(map) is similar to var m map[string]int and returning pointer to m. Here is the example since map behaves like empty while reading and nil during writing we see errors when write to a map happens before allocating memory.

creating map reference using new function

new(slice) creates a slice with zero value and returns pointer. In below example, append function checks size and capacity of slice and manages backing array before appending value. see the example below it looks like new() has initialized memory but that is actually done by append()

creating slices using new function

new(chan type) creates a channel with zero value that is a nil channel. Try running below example to see what happens when a value is sent to channel created with new() function.
Expect some errors!

create channel with new function

What is the specific problem new is trying to solve?

What ever new() function is doing can be achieved by initializing a data type and getting a pointer that data type. In below example both n and n1 looks same and n1 is much more readable and highlights that it is pointer

`
type Name struct {
FirstName string
LastNamae string
}

func main() {

n := new(Name)
n1 := &Name{}

fmt.Printf("Name: %v \n Name1: %v", n, n1)  
Enter fullscreen mode Exit fullscreen mode

}
`

new() in golang is different to new() in other programming languages always remember that new() in Go doesn't initialize memory but it only zeroes the value and return the pointer. To understand this better let us look at below example


var x *int
y := new(int)

What is the difference in above statements? first statement states that x can hold a pointer to integer data type. Second statement tells that zeroed value for int is set and pointer to that is returned. Run below example to understand

How is new() different? Playground Link

Conclusion

new() is different to make() and even without using new() zero valued initialization can be done. May be it is idiomatic way to use new() but other wise I don't see any specific use case which new() is solving. If you see any use case where only new() is able to solve please drop it in comments. Let me know what you think about the post.

References

discussion on new and make can be merged into one

Checkout my other article maps in Go

Top comments (3)

Collapse
 
sergsoares profile image
Sergio Soares

Good Explanation.

Collapse
 
labnan profile image
Labnan

thanks!

Collapse
 
sergsoares profile image
Sergio Soares

Good Explanation