reflect.Int() 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.Int() Function in Golang is used to get the v’s underlying value, as an int64. To access this function, one needs to imports the reflect package in the program.

Syntax:

func (v Value) Int() int64

Parameters: This function does not accept any parameter.

Return Value: This function returns the v’s underlying value, as an int64.

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



Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Golang program to illustrate 
// reflect.Int() Function 
   
package main
    
 import (
    "fmt"
    "reflect"
 )
   
 func main() {
           
    // use of Int() method
    fmt.Println(reflect.ValueOf(678).Int())
   
 

chevron_right


Output:

678

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Golang program to illustrate 
// reflect.Int() Function 
   
package main
    
 import (
    "fmt"
    "reflect"
 )
    
const a = 2
const b = 3
   
 func main() {
    fmt.Println("Number a : ", a)
    fmt.Println("Number b : ", b)
       
    c := a + b
           
    // use of Int() method
    fmt.Println(reflect.ValueOf(c).Int())
   
 

chevron_right


Output:

Number a :  2
Number b :  3
5



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.