make() vs new() - Similarities & Differences

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
now by this point you most likely have a use the built-in make function to make instantiations of what types like a channels however have you tried the built-in new function as well if you have do you fully understand the similarities and the differences between the two [Music] ninjas welcome back to the golang dojo your number one resource for all things go make sure to get your free golang tissues at golangdojo.com in this video we are talking about the make and new functions in go make a new r2 building functions to allocate memory to instantiate a variable for a given type even though these two functions do similar things that there are some pretty fundamental differences to use these two functions for the make function you will need to provide the type and arguments or arguments if needed and for the new function you would just need to provide the type the very first difference between these two functions would be the data types that they work with respectively the make function only works with slices and maps and channels while the new function technically works with any for example this is how you can use the make function to create slices and maps and channels but you can't use it to create integers however you can use the new function to create it the second difference between these two functions when allocating memory when creating variables for the given type would be the type that these two functions return respectively the make function will return the same type as at the given type while the new function will return a pointer to the type given for example let's say that we are giving the make function and the new function at the same time map the make function is going to return a variable that's the same as the given type being map and the new function is going to return a variable that is a mapper pointer the third difference between the make and new is make initializes and memory while new z-worlds and memory for example let's say that we are using the make function and the new function to try to create a map the main function will return a map variable and the new function will return a map pointer variable now notice that the make function will actually initialize the map while the new function will return a new pointer because the zero value of a mapper pointer would be new and the new function here wouldn't actually instantiate a map object but adjust it to create a new pointer if we comment now this line of instruction where we try to use the new function to create the map and use the variable returned by the make function the program is going to work perfectly fine however if we try to use the variable returned by the new function in a similar manner the program is unfortunately going to crash because the map object hasn't actually been initialized hasn't been instantiated all we get is a new pointer hence the panic right here the reason it panics the reason it crashes is because we are dereferencing a new pointer here which wouldn't give us a map object we can use normally now one thing i want to point out is that the zero value of a pointer variable that the new function returns can sometimes be useful rather than adjust it being a new for example the zero value of the buffer struct in the byte package is not new but an empty buffer ready to use instead for example the usage of this available return by the new function is not going to crash this time around if we run this program again we wouldn't see any panics or crashes so these are the similarities and the differences in between these two built-in functions that you will likely encounter one way or another in the future if you have any questions comments or suggestions for today's video make sure to leave them in the comment section down below make sure to get your free dolan q shoes out of golangdojo.com also like subscribe and turn on your notification bell and i'll see you ninjas in the next video [Music] you
Info
Channel: Golang Dojo
Views: 10,442
Rating: undefined out of 5
Keywords: golang, learn golang, go lang, go language, golang tutorial, go programming, golang for beginners, golang 2022, golang tutorial for beginners, programming functions, learn go, go tutorial, go for beginners, golang programming, golang functions, functions in golang, learn go programming, golang pointers, why golang, golang basics, programming basics, functions in go, golang tutorial for beginner, go, golang new, golang make, golang make vs new, make vs new, new vs make
Id: lB5fMdUU8DE
Channel Id: undefined
Length: 5min 6sec (306 seconds)
Published: Thu Apr 21 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.