mirror of
				https://github.com/3ybactuk/marketplace-go-service-project.git
				synced 2025-10-30 22:13:44 +03:00 
			
		
		
		
	
		
			
				
	
	
		
			20 lines
		
	
	
		
			400 B
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			20 lines
		
	
	
		
			400 B
		
	
	
	
		
			Go
		
	
	
	
	
	
| 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
 | |
| }
 | 
