package main
import "fmt"
type I interface {
fullName() string
}
type Person struct {
fname, lname string
}
func (p Person) fullName() string { // can not accept: (p *Person)
return p.fname + p.lname
}
func main() {
var name string = `hasan`
var p I = Person{fname: name, lname: `yousef`}
fmt.Printf("Your full name is: %v", p.fullName)
}
The output I got is:
Your full name is: 0x49c560
Which is not as expected, what is the 0x49c560 and how to get the correct output printed as string?
Yep, exactly as @Yamil_Bracho pointed out, you’ve forgot to actually call the function.
As in Go functions (and methods obviously) are first-class citizens, they can be used as values (assigned, passed as arguments, etc), that’s why it’s completely “legal” to do what you did, but this has different meaning
One other thing - your comment that says // can not accept: (p *Person) isn’t quite true - you actually can have pointer receivers! Your function could look like: