// Employee重载Human的Sayhi方法 func(e *Employee) SayHi() { fmt.Printf("Hi, I am %s, I work at %s. Call me on %s\n", e.name, e.company, e.phone) //此句可以分成多行 }
//Student实现BorrowMoney方法 func(s *Student) BorrowMoney(amount float32) { s.loan += amount // (again and again and...) }
//Employee实现SpendSalary方法 func(e *Employee) SpendSalary(amount float32) { e.money -= amount // More vodka please!!! Get me through the day! }
// 定义interface type Men interface { SayHi() Sing(lyrics string) Guzzle(beerStein string) }
type YoungChap interface { SayHi() Sing(song string) BorrowMoney(amount float32) }
type ElderlyGent interface { SayHi() Sing(song string) SpendSalary(amount float32) }
type Human struct { name string age int phone string }
type Student struct { Human //匿名字段 school string loan float32 }
type Employee struct { Human //匿名字段 company string money float32 }
//Human实现SayHi方法 func(h Human) SayHi() { fmt.Printf("Hi, I am %s you can call me on %s\n", h.name, h.phone) }
//Human实现Sing方法 func(h Human) Sing(lyrics string) { fmt.Println("La la la la...", lyrics) }
//Employee重载Human的SayHi方法 func(e Employee) SayHi() { fmt.Printf("Hi, I am %s, I work at %s. Call me on %s\n", e.name, e.company, e.phone) }
// Interface Men被Human,Student和Employee实现 // 因为这三个类型都实现了这两个方法 type Men interface { SayHi() Sing(lyrics string) }
funcmain() { mike := Student{Human{"Mike", 25, "222-222-XXX"}, "MIT", 0.00} paul := Student{Human{"Paul", 26, "111-222-XXX"}, "Harvard", 100} sam := Employee{Human{"Sam", 36, "444-222-XXX"}, "Golang Inc.", 1000} tom := Employee{Human{"Tom", 37, "222-444-XXX"}, "Things Ltd.", 5000}
//定义Men类型的变量i var i Men
//i能存储Student i = mike fmt.Println("This is Mike, a Student:") i.SayHi() i.Sing("November rain")
//i也能存储Employee i = tom fmt.Println("This is tom, an Employee:") i.SayHi() i.Sing("Born to be wild")
//定义了slice Men fmt.Println("Let's use a slice of Men and see what happens") x := make([]Men, 3) //这三个都是不同类型的元素,但是他们实现了interface同一个接口 x[0], x[1], x[2] = paul, sam, mike
funcmain() { list := make(List, 3) list[0] = 1// an int list[1] = "Hello"// a string list[2] = Person{"Dennis", 70}
for index, element := range list { if value, ok := element.(int); ok { fmt.Printf("list[%d] is an int and its value is %d\n", index, value) } elseif value, ok := element.(string); ok { fmt.Printf("list[%d] is a string and its value is %s\n", index, value) } elseif value, ok := element.(Person); ok { fmt.Printf("list[%d] is a Person and its value is %s\n", index, value) } else { fmt.Printf("list[%d] is of a different type\n", index) } } }
funcmain() { list := make(List, 3) list[0] = 1//an int list[1] = "Hello"//a string list[2] = Person{"Dennis", 70}
for index, element := range list{ switch value := element.(type) { caseint: fmt.Printf("list[%d] is an int and its value is %d\n", index, value) casestring: fmt.Printf("list[%d] is a string and its value is %s\n", index, value) case Person: fmt.Printf("list[%d] is a Person and its value is %s\n", index, value) default: fmt.Println("list[%d] is of a different type", index) } } }
type Interface interface { sort.Interface //嵌入字段sort.Interface Push(x interface{}) //a Push method to push elements into the heap Pop() interface{} //a Pop elements that pops elements from the heap }
type Interface interface { // Len is the number of elements in the collection. Len() int // Less returns whether the element with index i should sort // before the element with index j. Less(i, j int) bool // Swap swaps the elements with indexes i and j. Swap(i, j int) }
tag := t.Elem().Field(0).Tag //获取定义在struct里面的标签 name := v.Elem().Field(0).String() //获取存储在第一个字段里面的值
获取反射值能返回相应的类型和数值
1 2 3 4 5
var x float64 = 3.4 v := reflect.ValueOf(x) fmt.Println("type:", v.Type()) fmt.Println("kind is float64:", v.Kind() == reflect.Float64) fmt.Println("value:", v.Float())