Methods allow type structs defined in the func's receiver to access the func's stuff. Code Examples: package main import "fmt" type human struct { // struct gender string age int weight float64 } /*func (a *human) printdetails() { // method fmt.Printf("Name: Peter\nGender: %s\nAge: %d\nWeight: %g", a.gender, a.age, a.weight) }*/ func printdetails (a *human) { // non method fmt. Printf ( "Name: Peter \n Gender: %s \n Age: %d \n Weight: %g " , a.gender, a.age, a.weight) } func main () { var peter = new (human) * peter = human{ "Male" , 30 , 50.00 } //peter.printdetails() // Method printdetails (peter) // Non method } // methods are functions of a struct/class // can take a pointer receiver for the struct or a value receiver.
Comments
Post a Comment