赞
踩
package main
import (
"errors"
"fmt"
)
type item struct {
Name string
}
func (i item) String() string {
return fmt.Sprintf("item name:%v", i.Name)
}
type person struct {
Name string
Sex string
}
func (p person) String() string {
return fmt.Sprintf("person name:%v sex:%v", p.Name, p.Sex)
}
func Parse(i interface{}) interface{} {
switch i.(type) {
case string:
return &item{
Name: i.(string),
}
case []string:
data := i.([]string)
length := len(data)
if length == 2 {
return &person{
Name: data[0],
Sex: data[1],
}
} else {
return nil
}
default:
panic(errors.New("Type match miss"))
}
return nil
}
func main() {
p1 := Parse("apple").(*item)
fmt.Println(p1)
p2 := Parse([]string{"zhanghan", "man"}).(*person)
fmt.Println(p2)
fmt.Println(p1.Name)
fmt.Println(p2.Name)
fmt.Println(p2.Sex)
}
运行结果如下:
item name:apple
person name:zhanghan sex:man
apple
zhanghan
man
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。