Re: [go-nuts] List inside struct

2020-08-30 Thread Shyaka Rene
Thank you, it worked, everything is automatic in Go when you come from Rust
and Java, Thank you again

On Sun, Aug 30, 2020 at 2:59 AM Bakul Shah  wrote:

> 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
>
> On Aug 29, 2020, at 9:35 AM, Shyaka  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 := {
> 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.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/e8975da6-9af2-4660-8dbb-1a7e7e9b67cfn%40googlegroups.com
> 
> .
>
>
>

-- 
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/CAOix%2Bb4R13dQFJvfSXKERaWKaUQe8u-e1WMojVg2kKh6fuJFDg%40mail.gmail.com.


Re: [go-nuts] List inside struct

2020-08-29 Thread Bakul Shah
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 


> On Aug 29, 2020, at 9:35 AM, Shyaka  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 := {
> 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 
> .
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/golang-nuts/e8975da6-9af2-4660-8dbb-1a7e7e9b67cfn%40googlegroups.com
>  
> .

-- 
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.


Re: [go-nuts] List inside struct

2020-08-29 Thread Siddhesh Divekar
Shyaka,

The list package does all operation on list pointer.
In your case, I think you are passing list object instead of pointer.

You have defined your structure as:-

type Persons struct {
name  string
Names list.List < Its a list object and not a pointer to
list object.
}

Then you assign a list object to it.
p := {
name:  "",
Names: *list.New(),
}

All your operations are on Names list object & due to that its not getting
passed as reference to PushBack &
you don't see desired result.

The fix would be to make your list a pointer.

type Persons struct {
name  string
Names *list.List<- Make it a pointer.
}

func main() {

p := {
name:  "",
Names: list.New(), <- Create a new list
}

With these two changes its working as expected.


On Sat, Aug 29, 2020 at 2:01 PM burak serdar  wrote:

> On Sat, Aug 29, 2020 at 2:01 PM Shyaka  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 := {
> > name:  "",
> > Names: *list.New(),
> > }
>
> Your program doesn't work because of the above line. list.New()
> creates a new list containing the head/tail pointers within an Element
> instance, both pointing to the list created by list.New() by
> reference. They do not point to p.Names. When you add elements, those
> are added to the list returned by list.New(), not to Person.Names, but
> the size of Person.Names is incremented with each addition.
>
> Do not use list.New(), leave the Names uninitialized. It will
> initialize correctly the first time you add an element to it.
>
>
> > 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.
> > To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/e8975da6-9af2-4660-8dbb-1a7e7e9b67cfn%40googlegroups.com
> .
>
> --
> 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/CAMV2RqrH6v3wXMN1bw%3D2stUqfMTjB%2B%2BDmaQGYaV6OFw22af%2B_g%40mail.gmail.com
> .
>


-- 
-Siddhesh.

-- 
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/CAMjfk%2Bj_Td7y%2BRnPLBZ-%3DUFHfsNYndvn-g_TQQWYysheJk_6cQ%40mail.gmail.com.


Re: [go-nuts] List inside struct

2020-08-29 Thread burak serdar
On Sat, Aug 29, 2020 at 2:01 PM Shyaka  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 := {
> name:  "",
> Names: *list.New(),
> }

Your program doesn't work because of the above line. list.New()
creates a new list containing the head/tail pointers within an Element
instance, both pointing to the list created by list.New() by
reference. They do not point to p.Names. When you add elements, those
are added to the list returned by list.New(), not to Person.Names, but
the size of Person.Names is incremented with each addition.

Do not use list.New(), leave the Names uninitialized. It will
initialize correctly the first time you add an element to it.


> 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.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/golang-nuts/e8975da6-9af2-4660-8dbb-1a7e7e9b67cfn%40googlegroups.com.

-- 
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/CAMV2RqrH6v3wXMN1bw%3D2stUqfMTjB%2B%2BDmaQGYaV6OFw22af%2B_g%40mail.gmail.com.


Re: [go-nuts] List inside struct

2020-08-29 Thread Jan Mercl
On Sat, Aug 29, 2020 at 10:01 PM Shyaka  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.

I don't know why you code doesn't work, but as a first thing I just
changed it mechanically in way I would write it - and it seems to work
now: https://play.golang.org/p/NKyOQuM-GWc

-- 
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/CAA40n-W-wWeb9GSWLhMSyLtji-G5bFy8UNa%2BY4bKgXUm1UfBSg%40mail.gmail.com.