What is the Golang function new(Type) *Type?

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.

Prototype

func new(Type) *Type

Parameters

  • Type: The type of data for which the memory will be allocated.

Return type

new() returns a pointer of type T.

Code

package main
import "fmt"
func main() {
var a *int
var b *bool
var s *string
var arr *[10]int
a = 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

Copyright ©2025 Educative, Inc. All rights reserved