FAQ
Hi all,
Is it possible to have an anonymous interface implementation (something
similar to anonymous structs)? In the code below I'd like to have a foo
implementation without declaring a struct.

-------------------------------------------------------------------
package main

type T interface {
Foo()
}

func Bar(t T) {
t.Foo()
}

func main() {
Bar(nil) //need quick foo implementation here without declaring a type and
declaring a method on it
}
--------------------------------------------------------------------

Thanks!

--
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 [email protected].
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Dave Cheney at Jun 30, 2013 at 1:33 am
    // t satisfies the T interface
    type t int; func (*t) Foo() { }

    I don't think you can make this any smaller. You can only declare
    methods on types that you own (ie, declare in the package).


    On Sun, Jun 30, 2013 at 11:28 AM, wrote:
    Hi all,
    Is it possible to have an anonymous interface implementation (something
    similar to anonymous structs)? In the code below I'd like to have a foo
    implementation without declaring a struct.

    -------------------------------------------------------------------
    package main

    type T interface {
    Foo()
    }

    func Bar(t T) {
    t.Foo()
    }

    func main() {
    Bar(nil) //need quick foo implementation here without declaring a type and
    declaring a method on it
    }
    --------------------------------------------------------------------

    Thanks!

    --
    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 [email protected].
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    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 [email protected].
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJun 30, '13 at 1:28a
activeJun 30, '13 at 1:33a
posts2
users2
websitegolang.org

2 users in discussion

Dave Cheney: 1 post Shkarupin: 1 post

People

Translate

site design / logo © 2023 Grokbase