Función reflect.Uint() en Golang con ejemplos

El lenguaje Go proporciona una implementación de soporte incorporada de la reflexión en tiempo de ejecución y permite que un programa manipule objetos con tipos arbitrarios con la ayuda del paquete de reflexión. La función reflect.Uint() en Golang se usa para obtener el valor subyacente de v, como uint64. Para acceder a esta función, es necesario importar el paquete reflect en el programa.

Sintaxis:

func (v Value) Uint() uint64

Parámetros: Esta función no acepta ningún parámetro.

Valor devuelto: esta función devuelve el valor subyacente de v, como uint64.

Los siguientes ejemplos ilustran el uso del método anterior en Golang:

Ejemplo 1:

// 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"))
    
}

Producción:

3
5.599999904632568
GeeksforGeeks

Ejemplo 2:

// 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"))
    
}

Producción:

Value_1 : Geeks_1

Publicación traducida automáticamente

Artículo escrito por SHUBHAMSINGH10 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *