2
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"
}
4
  • 2
    A pointer isn't a boolean. Commented Aug 26, 2017 at 21:36
  • @user2357112 I understand that, what I don't understand is how to check in boolean fashion if the pointer points to a boolean. If *bool points to a bool that is true how would you check that in a condition.
    – Mo Ali
    Commented Aug 26, 2017 at 21:37
  • 3
    Dereference the pointer. Commented Aug 26, 2017 at 21:38
  • user2357112 Thanks that was the solution.
    – Mo Ali
    Commented Aug 26, 2017 at 21:45

2 Answers 2

9

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

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.