receiver's usage
package main
import "fmt"
type Person struct {
name string
age int
}
func (p *Person) greeting1() {
fmt.Println(p) //&{0}
fmt.Println(&p) //0xc000086018
fmt.Println("Hello~")
}
func (p Person) greeting2() {
fmt.Println(p) //{0}
fmt.Println(&p) //&{0}
fmt.Println("Hello~")
}
type Student struct {
//p Persion -> has a
Person // -> is a
school string
grade int
}
func test1(p Student) {
fmt.Println(p)
}
func test2(p *Student){
fmt.Println(p)
}
func main() {
var s Student
//s.p.greeting()
s.greeting1()
s.greeting2()
test1(s)
//test2(s) -> error
}
When I use function in golang, If I declare value variable, when I use function I made, I had to put only value variable on paramter. like,
a int= 10;
func func1(param int){
fmt.Println(fmt)
}
despite I declared Person as a value struct in Student struct, as you see my code, receiver functions(greeting1, greeting2) are getting two type parameters which are *Person, and Person. I cannot understand that why greeting1 function is functioning without error, despite I put value variable as a parameter. Thank you for you guys help.