Created
          January 23, 2015 16:32 
        
      - 
      
- 
        Save p886/44d73618ff04d2afb3a7 to your computer and use it in GitHub Desktop. 
    Basic Understanding of Pointers in Go
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | package main | |
| import "fmt" | |
| // Explanation: | |
| // changeIt() takes as argument a pointer to a thing | |
| // in line 14 we create such a pointer and pass it to changeIt(). | |
| // Hadn't we used a pointer but instead passed thing *normally*, | |
| // we would not have been able to observe the change | |
| // from "yolo" to "roflmao" in main | |
| func main() { | |
| thing := Thing{name: "yolo"} | |
| changeIt(&thing) | |
| fmt.Println(thing.name) | |
| } | |
| type Thing struct { | |
| name string | |
| } | |
| func changeIt(thing *Thing) { | |
| thing.name = "roflmao" | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment