golang将interface{}转换为struct
1 package main2
3 import (
4 "container/list"
5 "fmt"
6 "strconv"
7 )
8
9 type People struct {
10 Name string
11 Ageint
12 }
13
14 func main() {
15 // Create a new list and put some numbers in it.
16 l := list.New()
17 l.PushBack(People{"zjw", 1})
18
19 // Iterate through list and print its contents.
20 e := l.Front()
21 p, ok := (e.Value).(People)
22 if ok {
23 fmt.Println("Name:" + p.Name)
24 fmt.Println("Age:" + strconv.Itoa(p.Age))
25 } else {
26 fmt.Println("e is not an People")
27 }
28 }
页:
[1]