英文:
How check if a property was set in a struct
问题
我正在尝试找到如何检查结构属性是否被设置的方法,但是我找不到任何方法。
我期望像这样的代码,但是当然这是不起作用的:
type MyStruct struct {
property string
}
test := new(MyStruct)
if test.property {
//对此进行一些操作
}
英文:
I am trying to find how check if a structure property was set, but i cannot find any way.
I expect something like this but of corse this not works:
type MyStruct struct {
property string
}
test := new(MyStruct)
if test.property {
//do something with this
}
答案1
得分: 28
根据dyoo的说法,如果你的结构体属性是指针,你可以使用nil
。如果你想将它们保留为字符串,你可以与""
进行比较。这里是一个示例:
package main
import "fmt"
type MyStruct struct {
Property string
}
func main() {
s1 := MyStruct{
Property: "hey",
}
s2 := MyStruct{}
if s1.Property != "" {
fmt.Println("s1.Property已设置")
}
if s2.Property == "" {
fmt.Println("s2.Property未设置")
}
}
你可以在这里查看代码运行的示例:链接
英文:
Like dyoo said, you can use nil
if your struct properties are pointers. If you want to keep them as strings you can compare with ""
. Here is a sample:
package main
import "fmt"
type MyStruct struct {
Property string
}
func main() {
s1 := MyStruct{
Property: "hey",
}
s2 := MyStruct{}
if s1.Property != "" {
fmt.Println("s1.Property has been set")
}
if s2.Property == "" {
fmt.Println("s2.Property has not been set")
}
}
答案2
得分: 12
你可以使用指针及其nil
值来确定某个值是否已经设置。例如,如果你将结构体改为:
type MyStruct struct {
property *string
}
那么property
可以指向一个字符串值,这意味着它已经被设置了;或者它可以是nil
,这意味着它尚未被设置。这是protobuf库用来确定字段是否被设置的一种方法,你可以在https://code.google.com/p/goprotobuf/source/browse/README#83中看到。
英文:
You can use pointers and their nil
value to determine whether something has been set or not. For example, if you change your structure to
type MyStruct struct {
property *string
}
then property
can either be pointing to a string value, in which case it was set, or it can be nil
, in which case it hasn't been set yet. This is an approach that the protobuf library uses to determine whether fields are set or not, as you can see in https://code.google.com/p/goprotobuf/source/browse/README#83
答案3
得分: 5
除了Charlie的回答和对asafel的问题的(某种程度上的)回答之外,如果属性的类型不是字符串,你可以通过以下方式检查Go分配给该特定类型的默认值:
type MyStruct struct {
Str string
Bool bool
Int int
Uint uint
}
func main() {
s := MyStruct{}
fmt.Println(s.Str)
fmt.Println(s.Bool)
fmt.Println(s.Int)
fmt.Println(s.Uint)
}
输出:
""
false
0
0
你可以检查结构体中的值是否“已设置”或者“未设置”:
if (s.Bool == false) {
fmt.Println("Bool is either set to false, or wasn't set");
}
正如你可能已经注意到的,从技术上讲,你无法真正检查属性是否已设置,但我认为这是对Charlie回答的一个有价值的补充。
英文:
In addition to Charlie's answer and to (sort of) answer asafel's question, if the property's type isn't string, you can check the default value assign by Go to that particular type like this:
type MyStruct struct {
Str string
Bool bool
Int int
Uint uint
}
func main() {
s := MyStruct{}
fmt.Println(s.Str)
fmt.Println(s.Bool)
fmt.Println(s.Int)
fmt.Println(s.Uint)
}
Output:
""
false
0
0
And you can check whether the value "is set" in a struct or not
if (s.Bool == false) {
fmt.Println("Bool is either set to false, or wasn't set");
}
Well as you might have noticed, you can't technically really check whether the property was set or not, but I think this is a worth addition to Charlie's answer
答案4
得分: -17
另一种方法是将值设为私有,并在其上使用get/set方法。一个布尔值可以确定是否已设置。
type MyStruct struct {
isPropertySet bool
property string
}
func (my *MyStruct) SetProperty(val string) {
my.property = val
my.isPropertySet = true
}
func (my *MyStruct) IsPropertySet() bool {
return my.isPropertySet
}
英文:
Another way to do it would be to make the value private and use a get/set method on it. a bool can determine if set or not.
type MyStruct struct {
isPropertySet bool
property string
}
func (my *MyStruct) SetProperty(val string) {
my.property = val
my.isPropertySet = true
}
func (my *MyStruct) IsPropertySet() bool {
return my.isPropertySet
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论