FAQ
Hey guys,

So, I'm writing an MVC framework in Go. This framework uses a facade
object to manage controllers and proxies. The proxy type is specified as
an interface:

type Proxy interface {

GetName() string

Register() error

Remove()

SetFacade( f *Facade )

}


Then, in the facade, I have a function GetProxyByName


func ( f *Facade ) GetProxyByName( name string ) Proxy


Okay, this is good. If I call the function, I can get the appropriate proxy object. However, proxies have functions on them which are specific to the particular proxy. For instance, a database proxy might have a Find function. So, in my code, I'm attempting to cast the variable containing the proxy by interface into a variable containing the proxy by its concrete type, and I get an error. For instance:


proxy := p.facade.GetProxyByName( "database_proxy" )

dataproxy := proxy.(DatabaseProxy)


fails to compile with the error "impossible type assertion: proxy (type Proxy) cannot have dynamic type DatabaseProxy (missing GetName method)" even though GetName is a method of the DatabaseProxy type


func ( p *DatabaseProxy ) GetName() string


Can someone please explain to me how I can convert from an object of an interface type to its concrete type?


Thanks loads,

Lee



--

Search Discussions

  • Matt Kane's Brain at Sep 28, 2012 at 10:16 pm

    On Fri, Sep 28, 2012 at 6:11 PM, Lee Sylvester wrote:
    fails to compile with the error "impossible type assertion: proxy (type
    Proxy) cannot have dynamic type DatabaseProxy (missing GetName method)" even
    though GetName is a method of the DatabaseProxy type
    But it isn't!
    func ( p *DatabaseProxy ) GetName() string
    The receiver has pointer type, so you can type assert as (*DatabaseProxy).


    --
    matt kane's brain
    http://hydrogenproject.com

    --
  • Lee Sylvester at Sep 28, 2012 at 10:18 pm
    Thanks, yes, I just figured it out before you replied :-D Been pulling hair on that one for at least an hour :-S

    Cheers,
    Lee


    -----Original Message-----
    From: mkb.dirtyorg@gmail.com On Behalf Of Matt Kane's Brain
    Sent: 28 September 2012 23:16
    To: Lee Sylvester
    Cc: golang-nuts@googlegroups.com
    Subject: Re: [go-nuts] Interface to Concrete object
    On Fri, Sep 28, 2012 at 6:11 PM, Lee Sylvester wrote:
    fails to compile with the error "impossible type assertion: proxy
    (type
    Proxy) cannot have dynamic type DatabaseProxy (missing GetName
    method)" even though GetName is a method of the DatabaseProxy type
    But it isn't!
    func ( p *DatabaseProxy ) GetName() string
    The receiver has pointer type, so you can type assert as (*DatabaseProxy).


    --
    matt kane's brain
    http://hydrogenproject.com

    --

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedSep 28, '12 at 10:12p
activeSep 28, '12 at 10:18p
posts3
users2
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase