package tools import ( "fmt" "math" ) func SafeCastInt64ToUInt32(num int64) (uint32, error) { if num < 0 { return 0, fmt.Errorf("tried casting signed negative number to unsigned number") } if num > math.MaxUint32 { return 0, fmt.Errorf("tried casting larger number than uint32 can store") } // the bounds are checked, and cast should be safe. return uint32(num), nil // #nosec G115 }