*http://play.golang.org/p/4hHFRubSf5 <http://play.golang.org/p/4hHFRubSf5>*
I am having trouble with interface typing and type assertion.
Is it possible to do this graph with list?
I was able to run the code without any problem with array and slice but I
really want to do with list.
These are the functions that seem to cause panics.
func (A *Vertex) GetEdgesFromThisVertex() *chan *Edge* {
edgechan := make(chan *Edge)
go func() {
defer close(edgechan)
for e := A.EdgesFromThisVertex.Front(); e != nil; e = e.Next() {
*edgechan <- e.Value.(*Edge)*
}
}()
return edgechan
}
func (G *Graph) GetVertexByID(id string) **Vertex* {
for vtx := G.VertexList.Front(); vtx != nil; vtx = vtx.Next() {
if vtx.Value.(Vertex).ID == id {
return *vtx.Value.(*Vertex)*
}
}
return nil
}
And the error message is
panic: interface conversion: interface is *main.Vertex, not main.Vertex
What does this mean and how could I solve this? Basically my graph has three structures.
One for graph with vertices list, and vertex with ID and edge, predecessor lists, and edge with source and destination vertex and its weight value
type Graph struct {
VertexList *list.List
}
func NewGraph() *Graph {
return &Graph{
list.New(),
}
}
type Vertex struct {
ID string
Color string
EdgesFromThisVertex **list.List*
Predecessor **list.List*
}
func NewVertex(input_id string) *Vertex {
return &Vertex{
ID: input_id,
Color: "white",
EdgesFromThisVertex: *list.New(),*
Predecessor: *list.New(),*
}
}
type Edge struct {
SourceVertex *Vertex
DestinationVertex *Vertex
Weight int
}
I would greatly appreciate your response. Thanks! And happy new year!
--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.