top | item 32769533

(no title)

37ef_ced3 | 3 years ago

It's used like this

  p := new(int)
since you can't say

  p := &int{}

discuss

order

bilinguliar|3 years ago

Indeed, this may be the use case. I am curious how often you may need a pointer to an int.

marwatk|3 years ago

In things like rest APIs you need them quite a bit to distinguish between a value being the zero value and not present at all. Most libraries I've seen have an IntPointer or similar function exposed globally.

37ef_ced3|3 years ago

Are you seriously suggesting that the language should not have notation for allocating zeroed primitive types and receiving the address of the allocation?

  var (
      p1 = new(int)
      p2 = new(*int)
      p3 = new(**int)
      p4 = new(complex64)
      p5 = new(complex128)
  )
I feel like you must be joking. Should we say

  var (
      c complex128
      p = &c
  )
every time?