bool和*bool之间的区别是什么?

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

Difference between bool and *bool

问题

以下是翻译好的代码部分:

package main

import (
    "fmt"
    "github.com/aws/aws-sdk-go/aws"
    "github.com/aws/aws-sdk-go/aws/session"
    "github.com/aws/aws-sdk-go/service/route53"
)

func main() {
    sess := session.Must(session.NewSession())
    client := route53.New(sess)
    zones, err := client.ListHostedZones(&route53.ListHostedZonesInput{
        MaxItems: aws.String("100"),
    })
    if err != nil {
        fmt.Printf("发生错误")
    }
    fmt.Printf("%v", zones)
    if zones.IsTruncated {
        fmt.Printf("%v", zones.NextMarker)
    }
}

上述代码由于以下条件而失败。

if zones.IsTruncated {
    fmt.Printf("%v", zones.NextMarker)
}

错误信息为:

non-bool zones.IsTruncated (type *bool) used as if condition

我想知道的是为什么普通类型(bool)和指针类型(*bool)之间会有差异。我理解其中一个是指针值,但条件应该仍然有效。

以下是没有该条件的输出的一部分内容:

IsTruncated: true,
MaxItems: "100",
NextMarker: "Wouldn'tYouLikeToKnow"
}
英文:
package main

import (
    "fmt"
    "github.com/aws/aws-sdk-go/aws"
    "github.com/aws/aws-sdk-go/aws/session"
    "github.com/aws/aws-sdk-go/service/route53"
)

func main() {
    sess := session.Must(session.NewSession())
    client := route53.New(sess)
    zones, err := client.ListHostedZones(&route53.ListHostedZonesInput{
        MaxItems: aws.String("100"),
    })
    if err != nil {
        fmt.Printf("Error occurred")
    }
    fmt.Printf("%v", zones)
    if zones.IsTruncated {
        fmt.Printf("%v", zones.NextMarker)
    }
}

The code above fails due to the following condition.

if zones.IsTruncated {
    fmt.Printf("%v", zones.NextMarker)
}

with the result of

non-bool zones.IsTruncated (type *bool) used as if condition

What I want to know is why is there a difference between a regular type(bool) and type(*bool). I understand one is a pointer value however, the condition should still be useful.

Here is a tailed snippet of the output without the condition in place

IsTruncated: true,
MaxItems: "100",
NextMarker: "Wouldn'tYouLikeToKnow"
}

答案1

得分: 8

根据标准参考的说明:

> "if"语句根据布尔表达式的值指定条件执行两个分支。

所以,回答你的问题,if语句明确要求布尔表达式。*bool仍然不是bool类型。你需要显式地解引用指针(即使指针可能为nil,在这种情况下,你的程序将会出错)。

以下是你如何重写if语句的示例:

if *zones.IsTruncated {
    fmt.Printf("%v", zones.NextMarker)
}

但请确保它不是nil。

英文:

From standard reference:

> "If" statements specify the conditional execution of two branches according to the value of a boolean expression.

So, to answer your question, if statements explicitly require boolean expressions. *bool is still not bool. You need to explicitly dereference the pointer (which could even be nil in which case your program will panic).

Here's how you would re-write your if statement:

if *zones.IsTruncated {
    fmt.Printf("%v", zones.NextMarker)
}

but make sure it is not nil

答案2

得分: 2

你需要对一个结构体进行解引用。你需要使用 := 运算符来完成。类似这样 -

new_struct := *your_pointer

然后你可以使用 new_struct。

英文:

You need to dereference to a struct. You need to use the := operator to do so. Something like -

new_struct := *your_pointer

You would then use the new_struct instead.

huangapple
  • 本文由 发表于 2017年8月27日 05:32:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/45899840.html
匿名

发表评论

匿名网友

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

确定