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…


This content originally appeared on DEV Community and was authored by BhanuReddy

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)  

}
`

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


This content originally appeared on DEV Community and was authored by BhanuReddy


Print Share Comment Cite Upload Translate Updates
APA

BhanuReddy | Sciencx (2021-11-21T03:45:13+00:00) How is new() different in Go?. Retrieved from https://www.scien.cx/2021/11/21/how-is-new-different-in-go/

MLA
" » How is new() different in Go?." BhanuReddy | Sciencx - Sunday November 21, 2021, https://www.scien.cx/2021/11/21/how-is-new-different-in-go/
HARVARD
BhanuReddy | Sciencx Sunday November 21, 2021 » How is new() different in Go?., viewed ,<https://www.scien.cx/2021/11/21/how-is-new-different-in-go/>
VANCOUVER
BhanuReddy | Sciencx - » How is new() different in Go?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/11/21/how-is-new-different-in-go/
CHICAGO
" » How is new() different in Go?." BhanuReddy | Sciencx - Accessed . https://www.scien.cx/2021/11/21/how-is-new-different-in-go/
IEEE
" » How is new() different in Go?." BhanuReddy | Sciencx [Online]. Available: https://www.scien.cx/2021/11/21/how-is-new-different-in-go/. [Accessed: ]
rf:citation
» How is new() different in Go? | BhanuReddy | Sciencx | https://www.scien.cx/2021/11/21/how-is-new-different-in-go/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.