Advertisement
cwchen

[Go] Dynamically allocate a chunk of memory for an integer.

Sep 27th, 2017
1,024
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.31 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5.     "log"
  6. )
  7.  
  8. func main() {
  9.     // Allocate a chunk of memory.
  10.     nPtr := new(int)
  11.  
  12.     // Assign the value indirectly.
  13.     *nPtr = 2
  14.  
  15.     // Print out the address.
  16.     fmt.Println(nPtr)
  17.  
  18.     // Dereference the pointer to get the value.
  19.     if !(*nPtr == 2) {
  20.         log.Fatal("Wrong value")
  21.     }
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement