Nice post Shubam, but there's some problems with this implementation;
You cannot use Dog literal (type Dog) as type Pet in return argument, your interface Pet expect getAge method to return a string and you'r returning a int on your pet struct.
possible solution:
Change the interface to return a int on getAge or make your pet string return a string
The parameter type is wrong on your GetPet method because type is a keyword on golang
The method GetPet will fail if the "type" parameter is not "dog" or "cat", someone could pass "bird" to it.
possible solution:
funcGetPet(kindstring)(Pet,error){availablePets:=map[string]Pet{"cat":&Cat{},"dog":&Dog{},}ifpet,exist:=availablePets[kind];exist{returnpet,nil}returnnil,fmt.Errorf("there's no pet type \"%s\" available",kind)}
Nice post Shubam, but there's some problems with this implementation;
PetexpectgetAgemethod to return astringand you'r returning ainton yourpetstruct.possible solution:
The parameter
typeis wrong on yourGetPetmethod becausetypeis a keyword on golangThe method
GetPetwill fail if the "type" parameter is not "dog" or "cat", someone could pass "bird" to it.possible solution:
Ah, good catch! You're right about the type on the interface and the type keyword as an argument. Making the changes :D