This is a creation in Article, where the information may have evolved or changed.
The use of the method, see the code of this fearless
// method Definition of Golang // the methods in Golang are used on certain types of variables, so custom types can have methods, not just structs // Definition: func (recevier type) methodName (parameter list) (return value list) {} // the difference between a method and a function /* */
。。。。
Package Main//method Definition of Golang//the methods in Golang are used on certain types of variables, so custom types can have methods, not just structs//Definition: func (recevier type) methodName (parameter list) (return value list) {}Import"FMT"type integerintfunc (P integer) print () {fmt. Println ("P is:", p)}//here is a copy, want to change the value of P, need to pass the pointerFunc (P *integer)Set(b integer) {*p =B}type Studentstruct{Namestring AgeintscoreintSexint}//you need to accept the pointer *student (recipient), otherwise you cannot modify the valueFunc (P *student) init (namestring, ageint, scoreint) {P.name=name P.age=Age P.score=score Fmt. PRINTLN (P)}func (P Student)Get() Student {returnP}func Main () {varStu Student//Modify the spelling of the address (&stu). Init//but go can automatically know, the recipient is a pointer, here Stu to pass the addressStu.init ("Stu", -, About) STU1:= Stu.Get() fmt. Println (STU1)//Type Integer Method vara integer a= -A.print () a.Set( +) A.print ()}