go-方法method

//方法:函数指定接收者之后就是方法,只有某个具体的类型才能调用
package main
import "fmt"
type people struct {
  name  string
  gender string
}
//go中,约定使用类型首字母小写
 func (p people) dream() {
 fmt.Printf("name :%s", p.name)
 }
 func main() {
 var test = people{
   name:  "test",
   gender: "nan",
 }
 test.dream()
 }
//使用指针修改内容
func (p *people) dream() {
  p.name = "test2"
  fmt.Printf("name :%s", p.name)
}
func main() {
  var test = &people{
​    name:  "test",
​    gender: "nan",
  }
  test.dream()
}
posted @ 2020-07-12 21:54  kylingx  阅读(132)  评论(0编辑  收藏  举报