FAQ
Hi, I am having trouble to add default implementation of an interface, to
simulate inheritance. The concept is similar to java's abstract class
default implementation. But I don't know the right approach in Go.

For example, I want to build a graph of servers, on different racks and
data centers.
Currently, I have these code:

type Node struct{
parent *Node
children []*Node
}
//several default function implementations
func (*Node) AddChild(){
...
}

type Server struct {
Node
ipAddress string
...
}
type Rack struct {
Node
ipAddressRange string
...
}
type DataCenter struct {
Node
ipAddressRange string
...
}

However, server.Node.parent is a *Node, and can not be converted to a Rack
object.

Chris
http://weed-fs.googlecode.com

Search Discussions

  • DisposaBoy at Sep 2, 2012 at 7:27 am

    On Sunday, September 2, 2012 1:42:37 AM UTC+1, ChrisLu wrote:
    Hi, I am having trouble to add default implementation of an interface, to
    simulate inheritance. The concept is similar to java's abstract class
    default implementation. But I don't know the right approach in Go.

    For example, I want to build a graph of servers, on different racks and
    data centers.
    Currently, I have these code:

    type Node struct{
    parent *Node
    children []*Node
    }
    //several default function implementations
    func (*Node) AddChild(){
    ...
    }

    type Server struct {
    Node
    ipAddress string
    ...
    }
    type Rack struct {
    Node
    ipAddressRange string
    ...
    }
    type DataCenter struct {
    Node
    ipAddressRange string
    ...
    }

    However, server.Node.parent is a *Node, and can not be converted to a Rack
    object.

    Chris
    http://weed-fs.googlecode.com
    From the looks of it, the common thing is an ip address or range thereof so
    it sounds like you actually want Node to be an interface as opposed to a
    struct. e.g. you can create a node interface that all the relevant types
    implement direct or indirect via embedding a base struct. It's not clear
    what the relations are between the types but assuming an ipAddress can be
    represented as an ipAddressRange then it seems ok to put that in the base
    and then you need only implement an IpAddressRange, something
    like: http://play.golang.org/p/NhM-sqYpNFm
  • ChrisLu at Sep 2, 2012 at 8:29 am

    On Sunday, September 2, 2012 12:27:53 AM UTC-7, DisposaBoy wrote:

    On Sunday, September 2, 2012 1:42:37 AM UTC+1, ChrisLu wrote:

    Hi, I am having trouble to add default implementation of an interface, to
    simulate inheritance. The concept is similar to java's abstract class
    default implementation. But I don't know the right approach in Go.

    For example, I want to build a graph of servers, on different racks and
    data centers.
    Currently, I have these code:

    type Node struct{
    parent *Node
    children []*Node
    }
    //several default function implementations
    func (*Node) AddChild(){
    ...
    }

    type Server struct {
    Node
    ipAddress string
    ...
    }
    type Rack struct {
    Node
    ipAddressRange string
    ...
    }
    type DataCenter struct {
    Node
    ipAddressRange string
    ...
    }

    However, server.Node.parent is a *Node, and can not be converted to a
    Rack object.

    Chris
    http://weed-fs.googlecode.com
    From the looks of it, the common thing is an ip address or range thereof
    so it sounds like you actually want Node to be an interface as opposed to a
    struct. e.g. you can create a node interface that all the relevant types
    implement direct or indirect via embedding a base struct. It's not clear
    what the relations are between the types but assuming an ipAddress can be
    represented as an ipAddressRange then it seems ok to put that in the base
    and then you need only implement an IpAddressRange, something like:
    http://play.golang.org/p/NhM-sqYpNFm
    Thanks for replying! My concern is that if Node is an interface, I need to
    put the implementation of AddChild() to each of the struct Rack and
    DataCenter.

    Chris
    http://weed-fs.googlecode.com
  • Jesse McNelis at Sep 2, 2012 at 1:23 pm

    On Sun, Sep 2, 2012 at 6:29 PM, ChrisLu wrote:
    Thanks for replying! My concern is that if Node is an interface, I need to
    put the implementation of AddChild() to each of the struct Rack and
    DataCenter.
    AddChild() seems like it would be a two line method not exactly a
    worrying amount of code duplication.
    But you can just do this.

    type Node interface{
    AddChild(Node)
    }

    type Element struct{
    parent Node
    children []Node
    }

    func (*Element) AddChild(Node){}

    type Server struct {
    Element
    ipAddress string
    ...
    }
    type Rack struct {
    Element
    ipAddressRange string
    ...
    }
    type DataCenter struct {
    Element
    ipAddressRange string
    ...
    }

    Server, Rack, DataCenter all satisfy the Node interface.


    --
    =====================
    http://jessta.id.au
  • ChrisLu at Sep 3, 2012 at 9:26 pm
    Thanks! This helps a lot!

    Here is what I concluded: implementing the abstract class in Go should be
    split into two tasks.
    1. implement an interface
    2. implement an interface implementation, and mixin the implementation to
    the "inheriting" class.

    Chris
    On Sunday, September 2, 2012 6:23:27 AM UTC-7, Jesse McNelis wrote:

    On Sun, Sep 2, 2012 at 6:29 PM, ChrisLu <chri...@gmail.com <javascript:>>
    wrote:
    Thanks for replying! My concern is that if Node is an interface, I need to
    put the implementation of AddChild() to each of the struct Rack and
    DataCenter.
    AddChild() seems like it would be a two line method not exactly a
    worrying amount of code duplication.
    But you can just do this.

    type Node interface{
    AddChild(Node)
    }

    type Element struct{
    parent Node
    children []Node
    }

    func (*Element) AddChild(Node){}

    type Server struct {
    Element
    ipAddress string
    ...
    }
    type Rack struct {
    Element
    ipAddressRange string
    ...
    }
    type DataCenter struct {
    Element
    ipAddressRange string
    ...
    }

    Server, Rack, DataCenter all satisfy the Node interface.


    --
    =====================
    http://jessta.id.au
  • John Asmuth at Sep 2, 2012 at 11:03 am
    The right approach is to not use inheritance as a building block in your
    design.

    If you tell us what you want the code to do, functionally (rather than "I
    want it to have inheritance"), we can help you use the tools go provides
    effectively.
    On Saturday, September 1, 2012 8:42:37 PM UTC-4, ChrisLu wrote:

    Hi, I am having trouble to add default implementation of an interface, to
    simulate inheritance. The concept is similar to java's abstract class
    default implementation. But I don't know the right approach in Go.

    For example, I want to build a graph of servers, on different racks and
    data centers.
    Currently, I have these code:

    type Node struct{
    parent *Node
    children []*Node
    }
    //several default function implementations
    func (*Node) AddChild(){
    ...
    }

    type Server struct {
    Node
    ipAddress string
    ...
    }
    type Rack struct {
    Node
    ipAddressRange string
    ...
    }
    type DataCenter struct {
    Node
    ipAddressRange string
    ...
    }

    However, server.Node.parent is a *Node, and can not be converted to a Rack
    object.

    Chris
    http://weed-fs.googlecode.com

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedSep 2, '12 at 12:42a
activeSep 3, '12 at 9:26p
posts6
users4
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase