diff between make and new in Go

tags: learning go programming diff-between

content

makeMap := make(map[int]int)  // makeMap is a value
newMap := new(map[int]int)  // newMap is a pointer
makeMap[1] = 1  // allowed
newMap[1] = 1   // not allowed
 
makeMapPtr := &makeMap
(*newMap)[1] = 1 // not allowed, assignment to entry in nil map
(*makeMapPtr)[2] = 1 // allowed
  • make allows only maps, slices, channels, takes in Type, size, capacity, returns Type
  • new allows all type, it takes in Type, returns pointer to Type

up

down

reference