如何区分默认值和输入/更改的值。

huangapple go评论85阅读模式
英文:

How to distinguish between default value and inputed/changed

问题

我有一个结构体:

package main

type Test struct {
    FieldInt    int
    FieldBool   bool
    FieldString string
}

func main() {
    test := Test{}
    ...
}

这个结构体的默认值是:

  • FieldInt: 0
  • FieldBool: false
  • FieldString: ""

例如,用户可以更改FieldInt。它可以是正数、负数或0。

我想要检查用户是否更改了它(但是他/她可以设置为0):

if (test.FieldInt被更改) {
    // 做一些操作
}

但是如果用户输入了0,我无法区分默认值和输入/更改后的值。

如何区分默认值和输入/更改后的值?

英文:

I have a struct:

package main

type Test struct {
	FieldInt int
	FieldBool bool
	FieldString string
}

func main() {
    test := Test{}
    ...
}

Default values of this struct is:

  • FieldInt: 0
  • FieldBool: false
  • FieldString: ""

For example, a user can change FieldInt. It can be a positive, negative number and 0.

And I want to check, that the user changed it (but he/she can set 0):

if(test.FieldInt was changed) {
    // do something
}

But if the user inputted 0 then I can't distinguish between default value and inputted/changed one.

How to distinguish between default value and inputted/changed one?

答案1

得分: 6

一种常见的区分零值和未设置值的方法是使用指针:

type Test struct {
    FieldInt    *int
    FieldBool   *bool
    FieldString *string
}

然而,并不是每种情况下都适用这种解决方案。由于您没有告诉我们您的具体情况,所以我只是提供这个作为可能的解决方案之一。

英文:

One common way to distinguish between the zero value and an unset value is with pointers:

type Test struct {
    FieldInt    *int
    FieldBool   *bool
    FieldString *string
}

This won't be an appropriate solution for every situation, though, but since you haven't told us your precise situation, I offer this simply as one possible solution.

答案2

得分: 1

我个人倾向于Flimzy在这里提供的解决方案,因为它很简单。但是为了提供替代选项,你可以为每个属性添加一个标志,指示它是否已更新。

type Test struct {
    FieldInt int
    FieldBool bool
    FieldString string

    intUpdated bool
    boolUpdated bool
    stringUpdate bool
    // 由于这是用于管理状态的内部属性,我们不导出这些属性
}

对我来说,在Go代码中看到这样的东西相当奇怪,但在JS UI应用程序中,这是非常常见的(几乎所有现代应用程序都有“存储”,用于监视UI的状态,并通常具有指示UI中的值是否与存储中的值不同的标志)。对于后端系统,通常会有更多的开销,例如,很常见的是为存储最后更新时间和更新者的用户名而添加字段。实际上,在后端服务的数据存储中,这可能是常态。

英文:

I would personally tend towards the solution Flimzy provided here for it's simplicity. But just to provide alternative options, you could add a flag for each property to indicate whether or not it's been updated.

type Test struct {
    FieldInt int
    FieldBool bool
    FieldString string

    intUpdated bool
    boolUpdated bool
    stringUpdate bool
    // lets not export these though since it's for managing state
    // internally and consumers of this object shouldn't modify these values
}

To me this is rather a strange thing to see in Go code, however it would be very common place in JS UI apps (almost all modern apps have 'stores' which monitor the state of the UI and typically have flags to indicate whether a value in the UI has changed from what's in the store or not). For back end systems, you usually see even MORE overhead to go with this, for example it would be very common to have fields to store the last time of update and the name of the user who updated it. In fact, that is probably the norm within the data stores for back end services.

huangapple
  • 本文由 发表于 2017年7月20日 05:44:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/45201570.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定