Open In App

reflect.Uint() Function in Golang with Examples

Last Updated : 10 May, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report
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.Uint() Function in Golang is used to get v's underlying value, as a uint64. To access this function, one needs to imports the reflect package in the program.
Syntax:
func (v Value) Uint() uint64
Parameters: This function does not accept any parameter. Return Value: This function returns the v's underlying value, as a uint64.
Below examples illustrate the use of the above method in Golang: Example 1: C
// Golang program to illustrate
// reflect.Uint() Function
   
package main
   
import (
    "fmt"
    "reflect"
)
  
func sum(args []reflect.Value) []reflect.Value {
    a, b := args[0], args[1]
    if a.Kind() != b.Kind() {
        fmt.Println("??? ????.")
        return nil
    }
  
    switch a.Kind() {
    case reflect.Int, reflect.Int8, reflect.Int16,
                     reflect.Int32, reflect.Int64:
        return []reflect.Value{reflect.ValueOf(a.Int() + b.Int())}

    case reflect.Uint, reflect.Uint8, reflect.Uint16,
                      reflect.Uint32, reflect.Uint64:
        return []reflect.Value{reflect.ValueOf(a.Uint() + b.Uint())}

    case reflect.Float32, reflect.Float64:
        return []reflect.Value{reflect.ValueOf(a.Float() + b.Float())}

    case reflect.String:
        return []reflect.Value{reflect.ValueOf(a.String() + b.String())}
    default:
        return []reflect.Value{}
    }
}
  
func makeSum(fptr interface{}) {
    fn := reflect.ValueOf(fptr).Elem()
  
    v := reflect.MakeFunc(fn.Type(), sum)
  
    fn.Set(v)
}
   
// Main function
func main() {
  
    var intSum func(int, int) int64
    var floatSum func(float32, float32) float64
    var stringSum func(string, string) string
  
    makeSum(&intSum)
    makeSum(&floatSum)
    makeSum(&stringSum)
  
    fmt.Println(intSum(1, 2))
    fmt.Println(floatSum(2.1, 3.5))
    fmt.Println(stringSum("Geeksfor","Geeks"))
  
}
Output:
3
5.599999904632568
GeeksforGeeks
Example 2: C
// Golang program to illustrate
// reflect.Uint() Function
   
package main
   
import (
    "fmt"
    "reflect"
)
  
func sum(args []reflect.Value) []reflect.Value {
    a, b := args[0], args[1]
    if a.Kind() != b.Kind() {
        fmt.Println("??? ????.")
        return nil
    }
  
    switch a.Kind() {
    case reflect.Int, reflect.Int8, reflect.Int16,
                     reflect.Int32, reflect.Int64:
        return []reflect.Value{reflect.ValueOf(a.Int() + b.Int())}

    case reflect.Uint, reflect.Uint8, reflect.Uint16,
                      reflect.Uint32, reflect.Uint64:
        return []reflect.Value{reflect.ValueOf(a.Uint() + b.Uint())}

    case reflect.Float32, reflect.Float64:
        return []reflect.Value{reflect.ValueOf(a.Float() + b.Float())}

    case reflect.String:
        return []reflect.Value{reflect.ValueOf(a.String() + b.String())}
    default:
        return []reflect.Value{}
    }
}
  
func makeSum(fptr interface{}) {
    fn := reflect.ValueOf(fptr).Elem()
  
    v := reflect.MakeFunc(fn.Type(), sum)
  
    fn.Set(v)
}
   
// Main function
func main() {
 
    var stringSum func(string, string) string
 
    makeSum(&stringSum)
  
    fmt.Println(stringSum("Value_1 : ","Geeks_1"))
  
}
Output:
Value_1 : Geeks_1

Next Article
Article Tags :

Similar Reads