FAQ
This is the full code.
*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.

Search Discussions

  • Jesse McNelis at Jan 1, 2014 at 11:35 pm

    On 02/01/2014 10:20 AM, "Gyu-Ho Lee" wrote:
    This is the full code.
    http://play.golang.org/p/4hHFRubSf5

    I am having trouble with interface typing and type assertion.
    On line 25 you have 'Vertex' but you want '*Vertex'.

    --
    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.
  • Gyu-Ho Lee at Jan 1, 2014 at 11:38 pm
    Thank you so much, Jesse!

    I didn't notice that

    --
    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.
  • Adonovan at Jan 3, 2014 at 2:44 am

    On Wednesday, 1 January 2014 18:20:05 UTC-5, Gyu-Ho Lee wrote:
    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
    }
    FYI: while tempting, it's not idiomatic Go style to use channels simply for
    the ability to iterate over them. It's not efficient, and it can easily
    lead to an accumulation of idle goroutines: consider what happens when the
    caller of GetEdgesFromThisVertex discards the channel before reading to the
    end.

    Go doesn't have generic iterators like C++/Java/Python. When you want to
    return an iterable sequence from a function, you can return a slice (or
    append to a user-supplied one if you must avoid allocating) or define an
    ad-hoc iterator, like container/list.List does.

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

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJan 1, '14 at 11:20p
activeJan 3, '14 at 2:44a
posts4
users3
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase