// TODO what's the point! https://go.dev/tour/methods/10
// TODO In this example describe function uses Interface as argument. And this prevents multiple describe function. Is it a use case for interfacse (https://go.dev/tour/methods/11)
// TODO LOOK FOR HEAD-FIRST-GO EXAMPLES
// LINK https://www.digitalocean.com/community/tutorials/how-to-use-interfaces-in-go
// LINK https://duncanleung.com/understand-go-golang-interfaces/
// LINK https://research.swtch.com/interfaces
// TODO Understand interfaces more !
fmt.Printf("\n\n")
fmt.Println("If the concrete value inside the interface itself is nil, the method will be called with a nil receiver")
variI
vart*T
i=t
describe(i)
i.M()
i=&T{"hello"}
describe(i)
i.M()
fmt.Printf("\n")
fmt.Println("A nil interface value holds neither value nor concrete type")
fmt.Println("You can also use interfaces for getting a variable as argument and then deciding type of this variable. var.(type) returns interfaces type. And then you can check different types with switch-case")
somevar(61)
somevar("hello")
fmt.Printf("\n\n")
fmt.Println("One of the most ubiquitous interfaces is Stringer defined by the fmt package. A Stringer is a type that can describe itself as a string. he fmt package (and many others) look for this interface to print values.")
/*
typeStringerinterface{
String()string
}
*/
fmt.Println("This means you can change output style with String() method for a type")
myVarA:=Person{"Aliberk Sandıkçı",50}
myVarB:=Person{"Burak",100}
myVarC:=Person2{"Kemal",150}
fmt.Println(myVarA,myVarB,myVarC)
}
typePersonstruct{
Namestring
Ageint
}
func(pPerson)String()string{
returnfmt.Sprintf("%s + %d, ",p.Name,p.Age)
}
typePerson2struct{
Namestring
Ageint
}
func(pPerson2)String()string{
returnfmt.Sprintf("%s ! %d",p.Name,p.Age)
}
funcsomevar(iinterface{}){
switchv:=i.(type){
caseint:
fmt.Printf("INT: %v, %T\n",v,v)
casestring:
fmt.Printf("STRING: %v, %T\n",v,v)
default:
fmt.Printf("OTHER: %v, %T\n",v,v)
}
// fmt.Println( i.(type) ) // Can't be used outside of switches!