DEV Community

loading...

Using lists in go with structs

Carlos Alarcon B
Developer
・1 min read

This is a basic example in go using container/list and structs.



package main

import "fmt"
import "container/list"

type Person struct {
    name string
    age  int
}

func main() {

    person1 := Person{"John", 44}
    person2 := Person{"Julia", 22}

    list := list.New()
    list.PushBack(person1)
    list.PushBack(person2)

    // Iterate the list
    for e := list.Front(); e != nil; e = e.Next() {
        itemPerson := Person(e.Value.(Person))
        fmt.Println(itemPerson.name)
    }

}


Enter fullscreen mode Exit fullscreen mode

Discussion (1)

Collapse
renatosuero profile image
Renato Suero

I didn't know the package, thanks to share :)