This is a creation in Article, where the information may have evolved or changed.
Golang Walk (iii) list simple to use and interface{}
The code is as follows:
PackageMainImport("Container/list" "FMT")typePersonstruct{Ageint}funcMain () {l: = list. New () forI: =0; I <5; i++ {p: = person{age:i *Ten} l.pushback (P)} forI: =5; I <Ten; i++ {l.pushback (i)} I: =0 //Traversal forV: = L.front (); V! =Nil; v = v.next () {ifi >4{c: = V.value. (int) FMT. Println ("Get C:", c)}Else{p: = V.value. (person) fmt. Println ("Person.age:", P.age)} i++}}
The results of the operation are as follows:
010203040get5get6get7get8get9
Here's a look at interface{}
Element.value interface{} In list, where reflection is used to reflect Value to the appropriate type, such as: c: = V.value. (int)
People get used to their ideas.
You cannot use a strong turn like C, such as c: = Int (v.value), and a compilation error will occur.