Open In App

reflect.Bool() Function in Golang with Examples

Go language provides inbuilt support implementation of run-time reflection and allowing a program to manipulate objects with arbitrary types with the help of reflect package. The reflect.Bool() Function in Golang is used to get Value underlying value. To access this function, one needs to imports the reflect package in the program.

Syntax:



func (v Value) Bool() bool

Parameters: This function does not accept any parameters.

Return Value: This function returns the v’s underlying value.



Below examples illustrate the use of the above method in Golang:

Example 1:




// Golang program to illustrate
// reflect.Bool() Function
    
package main
    
import (
    "fmt"
    "reflect"
)
    
// Main function 
   
func main() {
    v := reflect.ValueOf(true)
   
    // use of Bool() method
    fmt.Printf("%v \n", v.Bool())
}        

Output:

true

Example 2:




// Golang program to illustrate
// reflect.Bool() Function
    
package main
    
import (
    "fmt"
    "reflect"
)
    
// Main function 
   
func main() {
    v := reflect.ValueOf(false)
      
    // use of Bool() method
    if v.Bool() == false{
        fmt.Printf("ValueOf(false).Bool() = false")
    }
}

Output:

ValueOf(false).Bool() = false

Article Tags :