Golang Walk (iii) list simple to use and interface{}

Source: Internet
Author: User
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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.