2

In Go I get that there are default values for types. Take int in this case which is initialised as a 0.

I have an issue where for me a 0 in an int can be a valid value so I need to check if it's been set by me or initialised as such. Is there any way to tell the difference between them at all?

Considering the following code... I need to be able to tell the difference between testIntOne and testIntTwo but they look the same!

package main

import "log"

type test struct {
    testIntOne int
    testIntTwo int
}

func main() {
    s := test{testIntOne: 0}

    log.Println(s)
}
Lee Armstrong
  • 11,420
  • 15
  • 74
  • 122
  • These two are totally the same and completely indistinguishable. Note that Go has formally no notion of "default value": All values come to existence with their _zero_ _value_. It makes absolutely no difference whether the compiler provides the zero value of `testIntOne` or if you do it yourself: You cannot tell. So: redesign. – Volker Dec 05 '16 at 09:05

1 Answers1

6

You can't tell the difference, it is not tracked whether a field (or a variable) has been set or not.

Using a pointer

You may use a pointer which has a nil zero value, so if not set, you can tell:

type test struct {
    testIntOne *int
    testIntTwo *int
}

func main() {
    s := test{testIntOne: new(int)}

    fmt.Println("testIntOne set:", s.testIntOne != nil)
    fmt.Println("testIntTwo set:", s.testIntTwo != nil)
}

Output (try it on the Go Playground):

testIntOne set: true
testIntTwo set: false

Of course new() can only be used to obtain a pointer to an int value being 0. See this question for more options: How do I do a literal *int64 in Go?

Using a method

You may also use a method to set a field, which could take care of additionally tracking the "isSet" property. In this case you must always use the provided method to set the field. Best is to make fields unexported, so others (outside your package) won't have direct access to them.

type test struct {
    testIntOne int
    testIntTwo int

    oneSet, twoSet bool
}

func (t *test) SetOne(i int) {
    t.testIntOne, t.oneSet = i, true
}

func (t *test) SetTwo(i int) {
    t.testIntTwo, t.twoSet = i, true
}

func main() {
    s := test{}
    s.SetOne(0)

    fmt.Println("testIntOne set:", s.oneSet)
    fmt.Println("testIntTwo set:", s.twoSet)
}

Output (try it on the Go Playground):

testIntOne set: true
testIntTwo set: false
Community
  • 1
  • 1
icza
  • 389,944
  • 63
  • 907
  • 827