There is a function that accepts a structure with a numeric field, in the function you need to check the following: if a value was written in the field, then return true, and if not, false. In this case, 0 can also be an assigned value, that is, if assigned to a field of 0, the function should return true.

type Data struct { Num int } func test(d Data) bool { // если d.Num значение присвоено, даже если это ноль то вернуть true // иначе false } 

    1 answer 1

    Your best bet is to use setters for this. As for the structure, you can either use pointers or add. Boolean field, or make a constructor that returns some special value of the field. That is either:

     type Data struct { numSet bool num int } func (d *Data) SetNum(n int) { d.numSet, d.num = true, n } 

    Or:

     type Data struct { num *int } func (d *Data) SetNum(n int) { d.num = &n } 

    Or:

     type Data struct { num int } func NewData() *Data { return &Data{ num: math.MaxInt32, } } 

    Make your own choices, depending on the situation. I personally consider the option with pointers the least preferable, since there is a danger of a null pointer and add. load on the garbage collector.