How is new() different in Go?
Tags: go, beginners, programming
Published: 2021-11-21 03:45:13


##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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1y5k7jkv88ij2x6vvspd.jpg)

##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](https://play.golang.org/p/AvIndq1Q7L8)

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](https://play.golang.org/p/IxfmM7WSR1x)

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](https://play.golang.org/p/Gx2fBYURPXQ)

##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)	
}
`

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](https://play.golang.org/p/kBHZtRF4qB3)
##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](https://groups.google.com/g/golang-nuts/c/kWXYU95XN04)

<span style="color:blue">Checkout my other article [maps in Go](https://dev.to/bhanu011/map-types-in-go-3h2i)</span>