Posts

part 1 receiver function

package main import ( "fmt" ) type Human struct { name string age int } type HumanList []Human //receiver on function , now any variable of type HumanList will have access to //printHuman function. eg. var1.printHuman() func (p HumanList ) printHuman(){ for _,v := range p {   fmt.Println(v.age,v.name)   } } func main() { //slice1 := []string{"b","c","d","g","f"} //for k,v := range slice1 { // fmt.Println(k,v) //} p1 := Human{name: "ram",age: 123} p2 := Human{name: "shyam",age: 213} p3 := Human{name: "hair",age: 112} var newslice HumanList newslice = append(newslice, p1,p2,p3) newslice.printHuman() }
Recent posts