The new
function in Golang allocates memory but does not initialize it as the make
function does. new
returns a pointer to the newly allocated zeroed value of type T
.
func new(Type) *Type
Type
: The type of data for which the memory will be allocated.new()
returns a pointer of type T
.
package mainimport "fmt"func main() {var a *intvar b *boolvar s *stringvar arr *[10]inta = new(int)b = new(bool)s = new(string)arr = new([10]int)fmt.Println(a, " ", *a)fmt.Println(s, " ",*s)fmt.Println(b, " ",*b)fmt.Println(arr, " ", *arr)}
In the code above, we declare pointers of types int
, bool
, and string
, and a pointer to an int
array of a
, b
, s
, and arr
respectively.
Then, we allocate memory for each of these pointers and print the values of the pointers and the data pointed to by the pointers.
Free Resources