Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
November 21, 2021 03:45 am GMT

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 
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

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


Original Link: https://dev.to/bhanu011/how-is-new-different-in-go-41lk

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To