Golang的Bool类型

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

Golang's Bool Type

问题

如何从一个函数返回 true 或 false,并进行检查。这段代码返回一个错误:mismatched types func() bool and bool

func d() bool {
    var e bool
    return e
}

if d == true {
    fmt.Printf("true")
}

如何修改代码以解决这个错误?

英文:

How do I return a true or false from a function and then check it. This code returns an error: mismatched types func() bool and bool

func d() bool {
    var e bool
    return e
}

if d == true {
    fmt.Printf("true")
}

答案1

得分: 3

你正在将实际函数与true进行比较,而不是函数的结果,你需要调用该函数,例如:

func d() bool {
    var e bool
    return e
}

if d() {
    fmt.Printf("true")
}

你需要调用函数d()来获取函数的结果,然后再与true进行比较。

英文:

You're comparing the actual function to true, not the function result, you need to call the function, e.g.

func d() bool {
    var e bool
    return e
}

if d() {
    fmt.Printf("true")
}

huangapple
  • 本文由 发表于 2016年11月13日 06:09:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/40568208.html
匿名

发表评论

匿名网友

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

确定