lkejfl
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
module main.go
|
||||
|
||||
go 1.24.4
|
||||
@@ -0,0 +1,37 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type Student struct {
|
||||
name string
|
||||
age int
|
||||
}
|
||||
|
||||
type Desriber interface {
|
||||
Describe()
|
||||
}
|
||||
|
||||
|
||||
func (std Student) Describe(){
|
||||
fmt.Printf("%s and %d y.o",std.name,std.age)
|
||||
}
|
||||
|
||||
func TypeDescriber(i interface{}){
|
||||
switch v := i.(type){
|
||||
case string:
|
||||
fmt.Println("I am string")
|
||||
case int:
|
||||
fmt.Println("I am int")
|
||||
case Desriber:
|
||||
fmt.Println("I am describer!")
|
||||
v.Describe()
|
||||
default:
|
||||
fmt.Println("Unknown type but I still work!")
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
TypeDescriber(9)
|
||||
std := Student{"Alex", 19}
|
||||
TypeDescriber(std)
|
||||
}
|
||||
Reference in New Issue
Block a user