Golang的if语句无法识别int32类型的0。

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

Golang if statement not seeing 0 for int32

问题

我有一个用golang编写的proto3/grpc函数。在一个switch语句中,if语句无法将int32的值0视为0。我在之前打印了该值,确实是0,但是if语句仍然执行。在我的代码中,我在注释中给出了输出。我知道对于int类型,nil值是0。如果我为lname和fname设置一个值,它们会按预期工作。非常感谢任何帮助。以下是我的输出:

map[fname: lname: email: id:0]
0
id = $1

以下是我的代码:

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 // "" (空)
    flds["lname"] = in.Lname // "" (空)
    flds["email"] = in.Email // "" (空)

    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:
            /////// 这个if语句有问题(v打印的值是0,但它在if语句中)
            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 = $1
    ...
英文:

I have proto3/grpc function written in golang. There is a if statement written in a switch that does't see an int32 as a value of 0 when the value is a 0. I print the value before and it's a 0 but the if statement runs anyway. In my code below I have the output in a comment. I know for a int the nil value is a 0. If I place a value for the lname, fname they work as it should. Any help appreciated. Here is my output:

map[fname: lname: email: id:0]
0
id = $1

Here is my code:

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 = $1
    ...

答案1

得分: 5

因为字面量0不是相同的类型。如果你这样写:

if v != int32(0) {

当值是int32类型时,它会按预期工作。不幸的是,你将所有的整数类型组合在一个case中,这将使得正确处理这个问题变得困难/不便。你可以尝试使用反射,在运行时将值与其类型的零值进行比较,使用reflect.Zero

英文:

Because the literal 0 is not the same type. If you do:

if v != int32(0) {

When the value is an int32, it works as expected. Unfortunately, you're combining all the int types in a single case, which will make this difficult/unwieldy to handle correctly. You could probably work something out using reflection to compare the value against the zero value for its type at runtime, using reflect.Zero.

huangapple
  • 本文由 发表于 2017年5月30日 21:16:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/44263381.html
匿名

发表评论

匿名网友

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

确定