Skip to content

Instantly share code, notes, and snippets.

@sandeepraju
Created March 3, 2015 11:47
Show Gist options
  • Save sandeepraju/6bc234f5caa8c504398d to your computer and use it in GitHub Desktop.
Save sandeepraju/6bc234f5caa8c504398d to your computer and use it in GitHub Desktop.
// pointer dereference - differences
package main
import "fmt"
type User struct {
name string
email string
}
func (u *User) Print() {
fmt.Println(u.name, "-", u.email)
}
func main() {
// with custom type
user := new(User) // user is a pointer of type User
(*user).name = "Sandeep Raju" // this works cool.
user.email = "me@sandeepraju.in" // this works as well.
user.Print()
fmt.Println(user)
fmt.Println(*user)
fmt.Println(&user)
// with builtin types
x := new(int)
*x = 100
fmt.Println(x)
fmt.Println(&x)
fmt.Println(*x)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment