FAQ
Hello,
Rookie Alert!

I have the following function type.

type callbackLogger func(vloginfo ViewLogInfo) error

My class users are supposed to set a reference to an implementation of it
(as a call back function to receive class status info).

I expected that the following implementation will work
var funclogger = func (vloginfo ViewLogInfo) error {
fmt.Println(vloginfo.Msg)
return nil
}

but I did not expect that a method implementation like the following will
even compile (the methodlogger has an extra parameter)

func (c *Controller) methodlogger (vloginfo ViewLogInfo) error {
fmt.Println(vloginfo.Msg)
return nil
}

but they both work. see http://play.golang.org/p/GuHg0QwHu0

Am I missing something?

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 golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Nodir Turakulov at Oct 22, 2015 at 6:32 pm
    In

    c.v.logger = c.methodlogger

    The receiver of the method is bound to the variable c, thus it can be
    converted to callbackLogger.
    It is equivalent to

    c.v.logger = func(vloginfo ViewLogInfo) {
       c.methodLogger(vLogInfo)
    }
    On Thu, Oct 22, 2015 at 11:26 AM DrGo wrote:

    Hello,
    Rookie Alert!

    I have the following function type.

    type callbackLogger func(vloginfo ViewLogInfo) error

    My class users are supposed to set a reference to an implementation of it
    (as a call back function to receive class status info).

    I expected that the following implementation will work
    var funclogger = func (vloginfo ViewLogInfo) error {
    fmt.Println(vloginfo.Msg)
    return nil
    }

    but I did not expect that a method implementation like the following will
    even compile (the methodlogger has an extra parameter)

    func (c *Controller) methodlogger (vloginfo ViewLogInfo) error {
    fmt.Println(vloginfo.Msg)
    return nil
    }

    but they both work. see http://play.golang.org/p/GuHg0QwHu0

    Am I missing something?

    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 golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
    --
    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/d/optout.
  • DrGo at Oct 22, 2015 at 6:46 pm
    totally makes sense now, thanks for the quick reply and nice explanation!

    Another question,
    I am using this approach because I anticipate that my class lib might end
    up accessed by code written in another language.
    What is the idiomatic way of implementing this in Golang (a) if my lib is
    only consumed by golang code (b) if I wanted it to be accessible from other
    languages eg Java or Swift?

    Thanks again,
    On Thursday, October 22, 2015 at 1:33:04 PM UTC-5, Nodir Turakulov wrote:

    In

    c.v.logger = c.methodlogger

    The receiver of the method is bound to the variable c, thus it can be
    converted to callbackLogger.
    It is equivalent to

    c.v.logger = func(vloginfo ViewLogInfo) {
    c.methodLogger(vLogInfo)
    }

    On Thu, Oct 22, 2015 at 11:26 AM DrGo <salah....@gmail.com <javascript:>>
    wrote:
    Hello,
    Rookie Alert!

    I have the following function type.

    type callbackLogger func(vloginfo ViewLogInfo) error

    My class users are supposed to set a reference to an implementation of it
    (as a call back function to receive class status info).

    I expected that the following implementation will work
    var funclogger = func (vloginfo ViewLogInfo) error {
    fmt.Println(vloginfo.Msg)
    return nil
    }

    but I did not expect that a method implementation like the following will
    even compile (the methodlogger has an extra parameter)

    func (c *Controller) methodlogger (vloginfo ViewLogInfo) error {
    fmt.Println(vloginfo.Msg)
    return nil
    }

    but they both work. see http://play.golang.org/p/GuHg0QwHu0

    Am I missing something?

    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 golang-nuts...@googlegroups.com <javascript:>.
    For more options, visit https://groups.google.com/d/optout.
    --
    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/d/optout.
  • Ian Lance Taylor at Oct 22, 2015 at 10:05 pm

    On Thu, Oct 22, 2015 at 11:46 AM, DrGo wrote:
    I am using this approach because I anticipate that my class lib might end up
    accessed by code written in another language.
    What is the idiomatic way of implementing this in Golang (a) if my lib is
    only consumed by golang code (b) if I wanted it to be accessible from other
    languages eg Java or Swift?
    Calling Go code from Java or Swift is not a trivial business. You'll
    want to look into https://godoc.org/golang.org/x/mobile/cmd/gobind ,
    which is experimental and under rapid development.

    Ian

    --
    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/d/optout.
  • Jason E. Aten at Oct 24, 2015 at 5:41 pm

    Another question,
    I am using this approach because I anticipate that my class lib might end
    up accessed by code written in another language.
    What is the idiomatic way of implementing this in Golang (a) if my lib is
    only consumed by golang code (b) if I wanted it to be accessible from other
    languages eg Java or Swift?
    There are many solutions, nothing standard, for cross language Remote
    Procedure Call (RPC) work. Apache thrift https://thrift.apache.org/ is
    perhaps one of the most mature across a spectrum of languages. For quick
    and need-it-today, you can often get alot done over http with json or
    msgpack for serialization before bothering with anything more complicated.
      Note that Swift is compatible with Objective-C when reading about which
    languages are supported by any given system.

    --
    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/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedOct 22, '15 at 6:26p
activeOct 24, '15 at 5:41p
posts5
users4
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase