Golang if 语句没有看到 0 for int32
Golang if statement not seeing 0 for int32
我有 proto3/grpc 函数是用 golang 写的。有一个 if 语句写在一个 switch 中,当值为 0 时,它不会将 int32 视为 0 值。我之前打印了该值,它是 0,但 if 语句仍然运行。在我下面的代码中,我在评论中有输出。我知道 int 的 nil 值为 0。如果我为 lname、fname 设置一个值,它们将按预期工作。任何帮助表示赞赏。这是我的输出:
map[fname: lname: email: id:0]
0
id =
这是我的代码:
func (s *server) GetUsers(ctx context.Context, in *userspb.User) (*userspb.Users, error) {
flds := make(map[string]interface{})
flds["id"] = in.Id // 0
flds["fname"] = in.Fname // "" (empty)
flds["lname"] = in.Lname // "" (empty)
flds["email"] = in.Email // "" (empty)
fmt.Println(flds) //map[lname: email: id:0 fname:]
var where bytes.Buffer
n := 0
for _, v := range flds {
switch v.(type) {
case string:
if v != "" {
n++
}
case int, int32, int64:
if v != 0 {
n++
}
}
}
calledvariables := make([]interface{}, 0)
i := 1
for k, v := range flds {
switch v.(type) {
case string:
if v != "" {
if i != 1 {
where.WriteString(" AND ")
}
ist := strconv.Itoa(i)
where.WriteString(k + " = $" + ist)
calledvariables = append(calledvariables, v)
i++
}
case int, int32, int64, uint32, uint64:
/////// THIS IF STATMENT IS THE ISSUE the ( v is printing the value of 0 and it's in the if statement )
if v != 0 {
fmt.Println(v) // 0
if i != 1 {
where.WriteString(" AND ")
}
ist := strconv.Itoa(i)
where.WriteString(k + " = $" + ist)
calledvariables = append(calledvariables, v)
i++
}
}
}
fmt.Println(where.String()) // id =
...
因为文字0
不是同一种类型。如果你这样做:
if v != int32(0) {
当值为 int32
时,它会按预期工作。不幸的是,您将所有 int 类型组合在一个案例中,这将使 difficult/unwieldy 能够正确处理。您可能会使用反射来解决一些问题,以便在运行时使用 reflect.Zero.
将值与其类型的零值进行比较
我有 proto3/grpc 函数是用 golang 写的。有一个 if 语句写在一个 switch 中,当值为 0 时,它不会将 int32 视为 0 值。我之前打印了该值,它是 0,但 if 语句仍然运行。在我下面的代码中,我在评论中有输出。我知道 int 的 nil 值为 0。如果我为 lname、fname 设置一个值,它们将按预期工作。任何帮助表示赞赏。这是我的输出:
map[fname: lname: email: id:0]
0
id =
这是我的代码:
func (s *server) GetUsers(ctx context.Context, in *userspb.User) (*userspb.Users, error) {
flds := make(map[string]interface{})
flds["id"] = in.Id // 0
flds["fname"] = in.Fname // "" (empty)
flds["lname"] = in.Lname // "" (empty)
flds["email"] = in.Email // "" (empty)
fmt.Println(flds) //map[lname: email: id:0 fname:]
var where bytes.Buffer
n := 0
for _, v := range flds {
switch v.(type) {
case string:
if v != "" {
n++
}
case int, int32, int64:
if v != 0 {
n++
}
}
}
calledvariables := make([]interface{}, 0)
i := 1
for k, v := range flds {
switch v.(type) {
case string:
if v != "" {
if i != 1 {
where.WriteString(" AND ")
}
ist := strconv.Itoa(i)
where.WriteString(k + " = $" + ist)
calledvariables = append(calledvariables, v)
i++
}
case int, int32, int64, uint32, uint64:
/////// THIS IF STATMENT IS THE ISSUE the ( v is printing the value of 0 and it's in the if statement )
if v != 0 {
fmt.Println(v) // 0
if i != 1 {
where.WriteString(" AND ")
}
ist := strconv.Itoa(i)
where.WriteString(k + " = $" + ist)
calledvariables = append(calledvariables, v)
i++
}
}
}
fmt.Println(where.String()) // id =
...
因为文字0
不是同一种类型。如果你这样做:
if v != int32(0) {
当值为 int32
时,它会按预期工作。不幸的是,您将所有 int 类型组合在一个案例中,这将使 difficult/unwieldy 能够正确处理。您可能会使用反射来解决一些问题,以便在运行时使用 reflect.Zero.