golang 中的struct理解
阿新 • • 發佈:2018-11-26
golang實驗程式碼
package main import("fmt") type Stu struct{ name string age int } func (stu *Stu)NewAge(age int)(PriAge int){ PriAge =age stu.age = age return } func (stu Stu)NewName(name string)(PriName string){ PriName = name stu.name = name return } func (stu Stu)info(){ fmt.Printf("%v\r\n",stu) } func main(){ xiaoming:=Stu{"xiaoming",23} xiaoming.info() xiaoming.NewAge(24) xiaoming.info() xiaoming.NewName("xm") xiaoming.info() }
輸出結果
C:\Users\haitun\Desktop>go run struct.go
{xiaoming 23}
{xiaoming 24}
{xiaoming 24}
理解:golang在 struct型別中
1.值型別可以呼叫接收者為指標的方法
2.呼叫接收者為指標的方法可以修改接收者