Unless there is a very strong reason to use a doubly linked list, you should 
just use a slice:

type Persons struct { name string; Names []string }

I redid your program using the above here: 
https://play.golang.org/p/x5I1wYiCNGA 

Sounds like you are coming from some object oriented language background.
Suggest reading "Effective Go": https://golang.org/doc/effective_go.html 
<https://golang.org/doc/effective_go.html>

> On Aug 29, 2020, at 9:35 AM, Shyaka <renek...@gmail.com> wrote:
> 
> Hello, I need help, I have a struct Person wiith list inside, I add 5 string 
> on list at the end I found that the list is nil, I don't know whre is the 
> error, any help is appreciated.
> 
> package main
> 
> import (
>     "container/list"
>     "fmt"
> )
> 
> type Persons struct {
>     name  string
>     Names list.List
> }
> 
> func main() {
> 
>     p := &Persons{
>         name:  "",
>         Names: *list.New(),
>     }
>     p.setName("John Peter")
>     p.add("one")
>     p.add("two")
>     p.add("three")
>     p.add("four")
>     p.add("five")
>     p.display()
> 
> }
> 
> func (p *Persons) setName(name string) {
>     p.name = name
> }
> func (p *Persons) add(name string) {
>     p.Names.PushBack(name)
> }
> func (p *Persons) display() {
>     fmt.Println(p.name)
>     for e := p.Names.Front(); e != nil; e = e.Next() {
>         fmt.Println(e.Value)
>     }
> }
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to golang-nuts+unsubscr...@googlegroups.com 
> <mailto:golang-nuts+unsubscr...@googlegroups.com>.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/golang-nuts/e8975da6-9af2-4660-8dbb-1a7e7e9b67cfn%40googlegroups.com
>  
> <https://groups.google.com/d/msgid/golang-nuts/e8975da6-9af2-4660-8dbb-1a7e7e9b67cfn%40googlegroups.com?utm_medium=email&utm_source=footer>.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/886C6A94-BC0A-4968-8A2A-74FD09215533%40iitbombay.org.

Reply via email to