FAQ
Hello.

I have an object
params map[string][]string
Is there any quick way to convert this object to any struct type, like json
unmarshal?

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

  • Sergey Gavruk at Oct 7, 2013 at 9:03 am
    My usecase:

    I want to parse request.Form (which is map[string][]string) to any struct.
    I can use reflection, but its very complicated. I hope there is any way to
    do it more simple.
    On Sunday, October 6, 2013 10:41:27 PM UTC+3, Sergey Gavruk wrote:

    Hello.

    I have an object
    params map[string][]string
    Is there any quick way to convert this object to any struct type, like
    json unmarshal?
    --
    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.
  • Tamás Gulácsi at Oct 7, 2013 at 11:53 am
    If you know the target struct beforehand, then you can write code for it. If not, then only reflection can help. Encoding/json.Marshal can marshal such datatype, then you can Unmarshal it to a properly tagged struct, but this is a very lazy solution.

    --
    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.
  • Derek Perkins at Jul 16, 2014 at 3:54 am

    On Monday, October 7, 2013 5:52:57 AM UTC-6, Tamás Gulácsi wrote:
    If you know the target struct beforehand, then you can write code for it.
    If not, then only reflection can help. Encoding/json.Marshal can marshal
    such datatype, then you can Unmarshal it to a properly tagged struct, but
    this is a very lazy solution.

    Tamás - Assuming I had a very simple struct like below, how would I write
    my UnmarshalJSON function to not use reflection?

    type Mytest struct {
    Field1 int32
    Field2 string
    }

    --
    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.
  • Tamás Gulácsi at Jul 16, 2014 at 7:06 am
    For such small structs you can write a json parser by hand, but using a generator (such as https://github.com/benbjohnson/megajson/blob/master/README.md) is less error prone, imho.

    --
    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.
  • Athiwat Chunlakhan at Oct 7, 2013 at 12:43 pm
    Take a look at http://www.gorillatoolkit.org/pkg/schema
    It's use to convert http stuff (which is a normal map)
    On Monday, October 7, 2013 2:41:27 AM UTC+7, Sergey Gavruk wrote:

    Hello.

    I have an object
    params map[string][]string
    Is there any quick way to convert this object to any struct type, like
    json unmarshal?
    --
    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.
  • Sergey Gavruk at Oct 7, 2013 at 1:09 pm
    Wow, that exactly what I need. Thank you!

    On Mon, Oct 7, 2013 at 3:43 PM, Athiwat Chunlakhan wrote:

    Take a look at http://www.gorillatoolkit.org/pkg/schema
    It's use to convert http stuff (which is a normal map)

    On Monday, October 7, 2013 2:41:27 AM UTC+7, Sergey Gavruk wrote:

    Hello.

    I have an object
    params map[string][]string
    Is there any quick way to convert this object to any struct type, like
    json unmarshal?
    --
    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.


    --
    Best regards,
    Sergey Gavruk

    --
    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.
  • Kevin Gillette at Oct 7, 2013 at 8:16 pm
    schema is fairly useful in practice, though it has a few limitations, such
    as a rather non-idiomatic style for registering custom unmarshalers (the
    idiomatic style would be to not register anything at all, but to define
    methods akin to json.Unmarshaler).
    On Monday, October 7, 2013 6:43:21 AM UTC-6, Athiwat Chunlakhan wrote:

    Take a look at http://www.gorillatoolkit.org/pkg/schema
    It's use to convert http stuff (which is a normal map)
    On Monday, October 7, 2013 2:41:27 AM UTC+7, Sergey Gavruk wrote:

    Hello.

    I have an object
    params map[string][]string
    Is there any quick way to convert this object to any struct type, like
    json unmarshal?
    --
    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
postedOct 6, '13 at 7:41p
activeJul 16, '14 at 7:06a
posts8
users5
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase