FAQ
Please see an example of my use case: http://play.golang.org/p/oM6ParA2AV

This example works because I use pointers or reference types which are all
nil when zero¹s. It works but it makes assigning a value nasty because you
always have to first create the variable and then assign the address of the
variable to the struct field.

Is there any other way I can do this without having to use pointers in my
struct? As you can see the end goal is to get a map (or in this case
url.values) returned which only contains the fields that have an actual
value.

Of course I understand that instead of the struct I could also make a map,
but as this is going to be a library package that would mean users of the
package have no way of knowing what fields are valid fields, so I would
really like to keep using the struct approach.

Thanks for any pointers ;) to a 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/d/optout.

Search Discussions

  • Harmen B at Sep 9, 2014 at 9:52 pm
    Personally I would just keep it as simple and possible and compare the
    fields against their zero value. So when t.Name == "" it's empty, or when
    t.Age == 0.
    All those pointers are ugly and messy and complicate things too much.

    If you _really_ want to find the difference between not set at all and ""
    you could keep an addition bool field for each field, a-la `struct {Name
    string; issetName bool}`, with logic to match those fields
    in structToUrlValues. But I seriously doubt it that's worth it.

    On Tue, Sep 9, 2014 at 11:40 PM, Sander van Harmelen wrote:

    Please see an example of my use case: http://play.golang.org/p/oM6ParA2AV

    This example works because I use pointers or reference types which are all
    nil when zero’s. It works but it makes assigning a value nasty because you
    always have to first create the variable and then assign the address of the
    variable to the struct field.

    Is there any other way I can do this without having to use pointers in my
    struct? As you can see the end goal is to get a map (or in this case
    url.values) returned which only contains the fields that have an actual
    value.

    Of course I understand that instead of the struct I could also make a map,
    but as this is going to be a library package that would mean users of the
    package have no way of knowing what fields are valid fields, so I would
    really like to keep using the struct approach.

    Thanks for any pointers ;) to a 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/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.
  • Péter Szilágyi at Sep 9, 2014 at 9:56 pm
    Definitely don't go down the zero-value means unset path! The moment your
    code tries to assign some default values to unset fields everything will
    come crashing down (see Google Datastore API).

    Either do pointers, or do flags (but imho, pointers are nice and simple).
    Yes, if would be even nicer without, but if I had to choose between
    pointers and doubling the struct fields... pointers any day :)
    On Wed, Sep 10, 2014 at 12:52 AM, Harmen B wrote:

    Personally I would just keep it as simple and possible and compare the
    fields against their zero value. So when t.Name == "" it's empty, or when
    t.Age == 0.
    All those pointers are ugly and messy and complicate things too much.

    If you _really_ want to find the difference between not set at all and ""
    you could keep an addition bool field for each field, a-la `struct {Name
    string; issetName bool}`, with logic to match those fields
    in structToUrlValues. But I seriously doubt it that's worth it.

    On Tue, Sep 9, 2014 at 11:40 PM, Sander van Harmelen wrote:

    Please see an example of my use case: http://play.golang.org/p/oM6ParA2AV

    This example works because I use pointers or reference types which are
    all nil when zero’s. It works but it makes assigning a value nasty because
    you always have to first create the variable and then assign the address of
    the variable to the struct field.

    Is there any other way I can do this without having to use pointers in my
    struct? As you can see the end goal is to get a map (or in this case
    url.values) returned which only contains the fields that have an actual
    value.

    Of course I understand that instead of the struct I could also make a
    map, but as this is going to be a library package that would mean users of
    the package have no way of knowing what fields are valid fields, so I would
    really like to keep using the struct approach.

    Thanks for any pointers ;) to a 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/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.
    --
    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.
  • Harmen B at Sep 9, 2014 at 9:59 pm

    On Tue, Sep 9, 2014 at 11:56 PM, Péter Szilágyi wrote:

    Definitely don't go down the zero-value means unset path!
    That really depends on the context, though. Sometimes it works just nicely.
    (But sometimes not, which you are right to point 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 golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Sander van Harmelen at Sep 9, 2014 at 10:10 pm
    Ah... I see we are all on the same page here ;)

    So maybe I should stick with the current approach... Hmm... :/

    Will do some more thinking...

    Sander

    On 9 sep. 2014, at 23:59, Harmen B wrote:



    On Tue, Sep 9, 2014 at 11:56 PM, Péter Szilágyi wrote:
    Definitely don't go down the zero-value means unset path!
    That really depends on the context, though. Sometimes it works just nicely.
    (But sometimes not, which you are right to point 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 golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Sander van Harmelen at Sep 9, 2014 at 10:02 pm
    Yeah, I thought of checking for the zero values, but for some cases that doesn't work. For example there are also bool values in the structs and their zero value is false... But of course I cannot assume that all false values are false because they where zero'd and should be excluded from the result.

    Adding the additional bool for each field doesn't sound usefull. The struct is already 20 to 30 fields for some structs, would become quite a mess to double that...

    But I do very much agree that all these pointers are ugle and keeping it simple would better...

    Sander

    On 9 sep. 2014, at 23:52, Harmen B wrote:

    Personally I would just keep it as simple and possible and compare the fields against their zero value. So when t.Name == "" it's empty, or when t.Age == 0.
    All those pointers are ugly and messy and complicate things too much.

    If you _really_ want to find the difference between not set at all and "" you could keep an addition bool field for each field, a-la `struct {Name string; issetName bool}`, with logic to match those fields in structToUrlValues. But I seriously doubt it that's worth it.

    On Tue, Sep 9, 2014 at 11:40 PM, Sander van Harmelen wrote:
    Please see an example of my use case: http://play.golang.org/p/oM6ParA2AV

    This example works because I use pointers or reference types which are all nil when zero’s. It works but it makes assigning a value nasty because you always have to first create the variable and then assign the address of the variable to the struct field.

    Is there any other way I can do this without having to use pointers in my struct? As you can see the end goal is to get a map (or in this case url.values) returned which only contains the fields that have an actual value.

    Of course I understand that instead of the struct I could also make a map, but as this is going to be a library package that would mean users of the package have no way of knowing what fields are valid fields, so I would really like to keep using the struct approach.

    Thanks for any pointers ;) to a 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/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.
  • Péter Szilágyi at Sep 9, 2014 at 9:53 pm
    I doubt it. If you are using purely values (i.e. not pointers), then there
    is no way for the system to differentiate between the default value of a
    type and the field being unset (Go uses the default value for uninitialized
    fields). This was the logical flaw behind the Go API to Google's services
    which makes it completely useless (in the case of the datastore). I guess
    with added runtime support it could be doable, but I don't think anyone's
    willing to pay the performance hit for such a feature :P
    On Wed, Sep 10, 2014 at 12:40 AM, Sander van Harmelen wrote:

    Please see an example of my use case: http://play.golang.org/p/oM6ParA2AV

    This example works because I use pointers or reference types which are all
    nil when zero’s. It works but it makes assigning a value nasty because you
    always have to first create the variable and then assign the address of the
    variable to the struct field.

    Is there any other way I can do this without having to use pointers in my
    struct? As you can see the end goal is to get a map (or in this case
    url.values) returned which only contains the fields that have an actual
    value.

    Of course I understand that instead of the struct I could also make a map,
    but as this is going to be a library package that would mean users of the
    package have no way of knowing what fields are valid fields, so I would
    really like to keep using the struct approach.

    Thanks for any pointers ;) to a 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/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.
  • Tahir at Sep 9, 2014 at 10:40 pm
    You need a constructor function for your objects. It means that you can
    keep the pointers in your struct and it is still only one function call to
    create a new instance of your object.

    Or as was said earlier, if you don't want pointers, you can store a bitmap
    in your struct to flag initialized fields but it means that you will have
    to create a constructor function AND getter/setters for each field that you
    want mutable I think.


    On Tuesday, September 9, 2014 10:41:22 PM UTC+1, Sander van Harmelen wrote:

    Please see an example of my use case: http://play.golang.org/p/oM6ParA2AV

    This example works because I use pointers or reference types which are all
    nil when zero’s. It works but it makes assigning a value nasty because you
    always have to first create the variable and then assign the address of the
    variable to the struct field.

    Is there any other way I can do this without having to use pointers in my
    struct? As you can see the end goal is to get a map (or in this case
    url.values) returned which only contains the fields that have an actual
    value.

    Of course I understand that instead of the struct I could also make a map,
    but as this is going to be a library package that would mean users of the
    package have no way of knowing what fields are valid fields, so I would
    really like to keep using the struct approach.

    Thanks for any pointers ;) to a 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/d/optout.
  • Tahir at Sep 9, 2014 at 10:58 pm
    Wait up, you just need setters for each field. :)
    On Tuesday, September 9, 2014 11:40:39 PM UTC+1, Tahir wrote:

    You need a constructor function for your objects. It means that you can
    keep the pointers in your struct and it is still only one function call to
    create a new instance of your object.

    Or as was said earlier, if you don't want pointers, you can store a bitmap
    in your struct to flag initialized fields but it means that you will have
    to create a constructor function AND getter/setters for each field that you
    want mutable I think.


    On Tuesday, September 9, 2014 10:41:22 PM UTC+1, Sander van Harmelen wrote:

    Please see an example of my use case: http://play.golang.org/p/oM6ParA2AV

    This example works because I use pointers or reference types which are
    all nil when zero’s. It works but it makes assigning a value nasty because
    you always have to first create the variable and then assign the address of
    the variable to the struct field.

    Is there any other way I can do this without having to use pointers in my
    struct? As you can see the end goal is to get a map (or in this case
    url.values) returned which only contains the fields that have an actual
    value.

    Of course I understand that instead of the struct I could also make a
    map, but as this is going to be a library package that would mean users of
    the package have no way of knowing what fields are valid fields, so I would
    really like to keep using the struct approach.

    Thanks for any pointers ;) to a 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/d/optout.
  • Sander van Harmelen at Sep 10, 2014 at 6:04 am
    Very nice one! I was already thinking about what I could do with custom
    setters, but your comment about using a single bitmap to keep track of the
    set values is really useful! Thanks for that one!

    I do have a new question about the custom setter on a struct field, but will
    post that in a new message as it’s actually a question on it’s own and will
    make it better searchable for others :)


    From: Tahir <welcometothebasement@gmail.com>
    Date: Wednesday 10 September 2014 00:58:00
    To: "golang-nuts@googlegroups.com" <golang-nuts@googlegroups.com>
    Subject: [go-nuts] Re: Is there a way to detect (using reflect maybe) if a
    field in a struct is set of not?

    Wait up, you just need setters for each field. :)
    On Tuesday, September 9, 2014 11:40:39 PM UTC+1, Tahir wrote:
    You need a constructor function for your objects. It means that you can keep
    the pointers in your struct and it is still only one function call to create a
    new instance of your object.

    Or as was said earlier, if you don't want pointers, you can store a bitmap in
    your struct to flag initialized fields but it means that you will have to
    create a constructor function AND getter/setters for each field that you want
    mutable I think.


    On Tuesday, September 9, 2014 10:41:22 PM UTC+1, Sander van Harmelen wrote:
    Please see an example of my use case: http://play.golang.org/p/oM6ParA2AV

    This example works because I use pointers or reference types which are all
    nil when zero’s. It works but it makes assigning a value nasty because you
    always have to first create the variable and then assign the address of the
    variable to the struct field.

    Is there any other way I can do this without having to use pointers in my
    struct? As you can see the end goal is to get a map (or in this case
    url.values) returned which only contains the fields that have an actual
    value.

    Of course I understand that instead of the struct I could also make a map,
    but as this is going to be a library package that would mean users of the
    package have no way of knowing what fields are valid fields, so I would
    really like to keep using the struct approach.

    Thanks for any pointers ;) to a 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/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.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedSep 9, '14 at 9:40p
activeSep 10, '14 at 6:04a
posts10
users4
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase