This file contains 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
// 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