mirror of
https://github.com/MADTeacher/go_basics.git
synced 2025-11-23 21:34:47 +02:00
31 lines
495 B
Go
31 lines
495 B
Go
package main
|
|
|
|
import "fmt"
|
|
|
|
type shape struct {
|
|
name string
|
|
}
|
|
|
|
func (s *shape) getName() string {
|
|
return s.name
|
|
}
|
|
|
|
func main() {
|
|
var firstInterface interface{} = shape{name: "Cube"}
|
|
value, ok := firstInterface.(string)
|
|
|
|
if ok {
|
|
fmt.Println(value) // работаем со значением
|
|
} else {
|
|
fmt.Println("Wrong type assertion!")
|
|
}
|
|
|
|
newValue, newOk := firstInterface.(shape)
|
|
|
|
if newOk {
|
|
fmt.Println(newValue.getName())
|
|
} else {
|
|
fmt.Println("Wrong type assertion!")
|
|
}
|
|
}
|