FAQ
I have a small JSON server, which receives a JSON blob. Inside the JSON is
a selector (string) for the function I want to run. All of the functions
take the same list of parameters.

Rather than build a "case" statement or cascading "if", I'd like to be able
to do something like this (pseudo-code):

selector := "a" // selector from the json packet, could be "a", "b",
"c"... indicating call to FuncA, FuncB, FuncC....
parm1 := "xxxx" // 1st parm from the json
parm2 := 75 // second parm....

and then just dispatch to FuncA, FuncB... by doing something like:

retval, err := dispatchtable[selector](parm1, parm2...)

Seems like this should be simple but didn't find anything in the docs or
this forum, at least that I recognized... still pretty new at understanding
Go.
Thanks,
Ken

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

  • Chris dollin at Dec 10, 2013 at 8:18 pm
    On 10 December 2013 20:15, Ken GoNoob wrote:

    I have a small JSON server, which receives a JSON blob. Inside the JSON is
    a selector (string) for the function I want to run. All of the functions
    take the same list of parameters.

    Rather than build a "case" statement or cascading "if", I'd like to be
    able to do something like this (pseudo-code):

    selector := "a" // selector from the json packet, could be "a", "b",
    "c"... indicating call to FuncA, FuncB, FuncC....
    parm1 := "xxxx" // 1st parm from the json
    parm2 := 75 // second parm....

    and then just dispatch to FuncA, FuncB... by doing something like:

    retval, err := dispatchtable[selector](parm1, parm2...)

    Seems like this should be simple but didn't find anything in the docs or
    this forum, at least that I recognized... still pretty new at understanding
    Go.

    Set up a map[string]typeoffunctionarguments with appropriate
    mappings.

    Chris

    --
    Chris "allusive" Dollin

    --
    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.
  • Ken GoNoob at Dec 10, 2013 at 8:27 pm
    This is a much better re-statement of my original question, thanks! As I
    noted, I had been looking for this sort of thing in the docs, but have been
    unable to find anything
    that looked (to me, anyway) as being related, being very NOOB with Go
    syntax and types.
    On Tuesday, December 10, 2013 3:18:54 PM UTC-5, chris dollin wrote:


    Set up a map[string]typeoffunctionarguments with appropriate
    mappings.

    Chris

    --
    Chris "allusive" Dollin
    --
    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.
  • Ken Allen at Dec 10, 2013 at 8:35 pm
    Seems Chris Dollin already said what I was going to say but here's an
    example: http://play.golang.org/p/bK8tolIWRd
    On Tuesday, December 10, 2013 3:27:42 PM UTC-5, Ken GoNoob wrote:

    This is a much better re-statement of my original question, thanks! As I
    noted, I had been looking for this sort of thing in the docs, but have been
    unable to find anything
    that looked (to me, anyway) as being related, being very NOOB with Go
    syntax and types.
    On Tuesday, December 10, 2013 3:18:54 PM UTC-5, chris dollin wrote:


    Set up a map[string]typeoffunctionarguments with appropriate
    mappings.

    Chris

    --
    Chris "allusive" Dollin
    --
    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.
  • Ken Allen at Dec 10, 2013 at 8:38 pm
    Actually I'm surprised that worked the way I wrote it because I didn't have
    to explicitly convert the function literal to a JsonFunc. Since when has
    that not been necessary?
    On Tuesday, December 10, 2013 3:35:00 PM UTC-5, Ken Allen wrote:

    Seems Chris Dollin already said what I was going to say but here's an
    example: http://play.golang.org/p/bK8tolIWRd
    On Tuesday, December 10, 2013 3:27:42 PM UTC-5, Ken GoNoob wrote:

    This is a much better re-statement of my original question, thanks! As I
    noted, I had been looking for this sort of thing in the docs, but have been
    unable to find anything
    that looked (to me, anyway) as being related, being very NOOB with Go
    syntax and types.
    On Tuesday, December 10, 2013 3:18:54 PM UTC-5, chris dollin wrote:


    Set up a map[string]typeoffunctionarguments with appropriate
    mappings.

    Chris

    --
    Chris "allusive" Dollin
    --
    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.
  • Ken GoNoob at Dec 10, 2013 at 8:48 pm
    That looks like what I need, thanks, Ken! In the playground example the
    function "a" is very small and easy to inline; is there a way to declare
    the function "externally" and still get it's
    signature into the function map? Looks good though; I'm nbow officially
    getting pretty good at reading (tho no writing) Go... - Ken
    On Tuesday, December 10, 2013 3:35:00 PM UTC-5, Ken Allen wrote:

    Seems Chris Dollin already said what I was going to say but here's an
    example: http://play.golang.org/p/bK8tolIWRd
    On Tuesday, December 10, 2013 3:27:42 PM UTC-5, Ken GoNoob wrote:

    This is a much better re-statement of my original question, thanks! As I
    noted, I had been looking for this sort of thing in the docs, but have been
    unable to find anything
    that looked (to me, anyway) as being related, being very NOOB with Go
    syntax and types.
    On Tuesday, December 10, 2013 3:18:54 PM UTC-5, chris dollin wrote:


    Set up a map[string]typeoffunctionarguments with appropriate
    mappings.

    Chris

    --
    Chris "allusive" Dollin
    --
    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.
  • Ken Allen at Dec 10, 2013 at 9:12 pm
    Just declare the function normally and refer to it by name when calling
    RegisterFunc: http://play.golang.org/p/PLxabYdFhR

    I also moved the call to RegisterFunc into init because that's the closest
    you'll get in go to having these functions automatically registered. Every
    go file can have an init function and they are all called when your program
    starts - it's a perfect place to do things like initializing that map or
    registering these kinds of things.
    On Tuesday, December 10, 2013 3:48:29 PM UTC-5, Ken GoNoob wrote:

    That looks like what I need, thanks, Ken! In the playground example the
    function "a" is very small and easy to inline; is there a way to declare
    the function "externally" and still get it's
    signature into the function map? Looks good though; I'm nbow officially
    getting pretty good at reading (tho no writing) Go... - Ken
    On Tuesday, December 10, 2013 3:35:00 PM UTC-5, Ken Allen wrote:

    Seems Chris Dollin already said what I was going to say but here's an
    example: http://play.golang.org/p/bK8tolIWRd
    On Tuesday, December 10, 2013 3:27:42 PM UTC-5, Ken GoNoob wrote:

    This is a much better re-statement of my original question, thanks! As I
    noted, I had been looking for this sort of thing in the docs, but have been
    unable to find anything
    that looked (to me, anyway) as being related, being very NOOB with Go
    syntax and types.
    On Tuesday, December 10, 2013 3:18:54 PM UTC-5, chris dollin wrote:


    Set up a map[string]typeoffunctionarguments with appropriate
    mappings.

    Chris

    --
    Chris "allusive" Dollin
    --
    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.
  • Ken MacDonald at Dec 10, 2013 at 9:18 pm
    Thanks, Ken Andrey, Chris!

    Good tip on the init() function... an example I ran across had one, but no
    indication as to how it was invoked. I'll build me one o' them!
    Ken
    On Tue, Dec 10, 2013 at 4:11 PM, Ken Allen wrote:

    http://play.golang.org/p/PLxabYdFhR
    --
    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.
  • Andrey mirtchovski at Dec 10, 2013 at 8:23 pm
    the best but perhaps most elaborate example is in the 'go' command
    source code. each individual go subcommand defines a separate Command
    struct:

    http://golang.org/src/cmd/go/main.go#L29

    the static set of usable commands is defined below as an array (in
    your case you can use a map):

    http://golang.org/src/cmd/go/main.go#L76

    and the Run function for each of those is assigned at runtime using init():

    http://golang.org/src/cmd/go/build.go#L104

    --
    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
postedDec 10, '13 at 8:15p
activeDec 10, '13 at 9:18p
posts9
users4
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase