github.com/haraldrudell/parl@v0.4.176/preflect/int.go (about) 1 /* 2 © 2022–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/) 3 ISC License 4 */ 5 6 package preflect 7 8 import ( 9 "reflect" 10 ) 11 12 // IntValue returns the integer value of value 13 // - hasValue is true and u64 is initialized for any integer, pointer or nil 14 // - for signed integer types, isSigned is true and i64 has value 15 // - isPointer indicates that u64 holds a pointer value 16 // - nil returns: u64 = 0, hasValue true, isPointer true, isSigned false 17 // - superseded by non-reflection version [github.com/haraldrudell/parl/ints.IntProperties] 18 func IntValue(value any) (u64 uint64, i64 int64, hasValue, isSigned, isPointer bool) { 19 var typeOf reflect.Type = reflect.TypeOf(value) 20 if hasValue = typeOf == nil; hasValue { 21 isPointer = true 22 return // nil value return 23 } 24 var reflectValue reflect.Value = reflect.ValueOf(value) 25 var reflectKind reflect.Kind = typeOf.Kind() 26 switch reflectKind { 27 case reflect.Ptr: 28 u64 = uint64(reflectValue.Pointer()) 29 hasValue = true 30 isPointer = true 31 case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uint: 32 u64 = reflectValue.Uint() 33 hasValue = true 34 case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Int: 35 // interface conversion: interface {} is pcap.activateError, not int64 36 i64 = reflectValue.Int() 37 u64 = uint64(i64) 38 hasValue = true 39 isSigned = true 40 } 41 return 42 }