Howto access an item retrieved from a List [SOLVED]

container/list end works fine adding structures, but when I retrieve a structure I cant access its fields. Obviously I am missing something. Code follows; also on https://play.golang.org/p/Zr0YOy9eLtD

  package main

import ("fmt";"container/list";"os")

type testRec struct{ name string; age int }

var d0= testRec {name: "Bill", age: 29}
var d1= testRec {name: "Bob", age: 30}

var list0 *list.List

func main(){
list0=list.New(); list0.Init();

p0:=list0.PushBack(&d0); p1:=list0.PushBack(&d1);

fmt.Println("Items on list: ",list0.Len());
fmt.Println("p0: ",p0,"\np1: ",p1)		// force a reference

// now process a list item
r0:=list0.Front(); fmt.Println("r0: ",r0)		// should match p0
data:=r0.Value; fmt.Println(data);
_,ok:=data.(testRec)
if ok==false {  fmt.Println("Wrong type"); os.Exit(1);}
//fmt.Println("Name: ",v0.name);
fmt.Println("Exit OK")
os.Exit(0);
}

And when we run it

bjc@sol2 ~/go5 $ go run test1.go
Items on list:  2
p0:  &{0xc00005a1b0 0xc00005a150 0xc00005a150 0x546410} 
p1:  &{0xc00005a150 0xc00005a180 0xc00005a150 0x546430}
r0:  &{0xc00005a1b0 0xc00005a150 0xc00005a150 0x546410}
&{Bill 29}
Wrong type
exit status 1

So how do I coerce the type to my structure ? (I have tried several alternaives, but noe that the compiler accepts.)

Any help appreciated

You can access the Value of the Element

package main

import (
	"container/list"
	"fmt"
)

type testRec struct {
	name string
	age  int
}

func (tr testRec) String() string {
	return fmt.Sprintf("testRec(name=%v, age=%v)", tr.name, tr.age)
}

var d0 = testRec{name: "Bill", age: 29}
var d1 = testRec{name: "Bob", age: 30}

var list0 *list.List

func main() {
	list0 = list.New()
	list0.Init()

	p0 := list0.PushBack(&d0)
	p1 := list0.PushBack(&d1)

	fmt.Printf("p0: %s\n", p0.Value)
	fmt.Printf("p1: %s\n", p1.Value)
}

Output:

p0: testRec(name=Bill, age=29)
p1: testRec(name=Bob, age=30)

https://play.golang.com/p/cwUvPAVo8DO

Thanks Lutz. Your Print routine also works on a item read from the list. My problem is that I want to put a record on the list and then read it back. I attempted to get the record from the element. Go playground https://play.golang.org/p/_7PEB2I7nz7.

package main

import ("fmt";"container/list";"os")

type testRec struct{ name string; age int }

 func (tr testRec) String() string {
return fmt.Sprintf("testRec(name=%v, age=%v)", tr.name, tr.age)
}

func (tr testRec) GetFromElement() testRec {
return testRec{name: tr.name, age: tr.age}
}

var d0= testRec {name: "Bill", age: 29}
var d1= testRec {name: "Bob", age: 30}

var list0 *list.List

func main(){
list0=list.New(); list0.Init();

p0:=list0.PushBack(&d0); p1:=list0.PushBack(&d1);

fmt.Println("Items on list: ",list0.Len());

// now process a list item
r0:=list0.Front();
fmt.Printf("r0:p %s\n",r0.Value);
v0:=r0.Value.GetFromElement();
fmt.Println("v0: name: ",v0.name," Age: ",v0.age);
fmt.Println("Exit OK")
os.Exit(0);
}

I get a compile time error

prog.go:31:14: r0.Value.GetFromElement undefined (type interface {} is interface with no methods)

Go build failed.

I don’t know what GetFromElement is supposed to be.

Try a type assertion on Value:

package main

import (
	"container/list"
	"fmt"
)

type testRec struct {
	name string
	age  int
}

func (tr testRec) String() string {
	return fmt.Sprintf("testRec(name=%v, age=%v)", tr.name, tr.age)
}

var d0 = testRec{name: "Bill", age: 29}
var d1 = testRec{name: "Bob", age: 30}

var list0 *list.List

func main() {
	list0 = list.New()
	list0.Init()

	list0.PushBack(&d0)
	list0.PushBack(&d1)

	for e := list0.Front(); e != nil; e = e.Next() {
		tr, ok := e.Value.(*testRec)
		if ok == false {
			fmt.Println("List is corrupt.  Consider restarting the system")
			break
		}
		fmt.Println(tr.name, tr.age)
	}

}

https://play.golang.com/p/UZZA-W1AXYq

Looks good. I will test it. Thanks

Exactly what was required to caste the item.

Thanks again

1 Like

Suggest adding error checking

// now process the list items

for e:=list0.Front(); e!=nil; e=e.Next(){
	tr,ok:=e.Value.(*testRec)
	if ok==false{
		fmt.Println("List is corrupt.  Consider restarting the system");
		break;}
	fmt.Println(tr.name,tr.age);
}
1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.