mirror of
https://github.com/teivah/100-go-mistakes.git
synced 2026-06-21 00:47:11 +08:00
50 lines
740 B
Go
50 lines
740 B
Go
package main
|
|
|
|
import "math"
|
|
|
|
func Inc32(counter int32) int32 {
|
|
if counter == math.MaxInt32 {
|
|
panic("int32 overflow")
|
|
}
|
|
return counter + 1
|
|
}
|
|
|
|
func IncInt(counter int) int {
|
|
if counter == math.MaxInt {
|
|
panic("int overflow")
|
|
}
|
|
return counter + 1
|
|
}
|
|
|
|
func IncUint(counter uint) uint {
|
|
if counter == math.MaxUint {
|
|
panic("uint overflow")
|
|
}
|
|
return counter + 1
|
|
}
|
|
|
|
func AddInt(a, b int) int {
|
|
if a > math.MaxInt-b {
|
|
panic("int overflow")
|
|
}
|
|
|
|
return a + b
|
|
}
|
|
|
|
func MultiplyInt(a, b int) int {
|
|
if a == 0 || b == 0 {
|
|
return 0
|
|
}
|
|
|
|
result := a * b
|
|
if a == 1 || b == 1 {
|
|
return result
|
|
}
|
|
if a == math.MinInt || b == math.MinInt {
|
|
panic("integer overflow")
|
|
}
|
|
if result/b != a {
|
|
panic("integer overflow")
|
|
}
|
|
return result
|
|
}
|