Skip to content

Instantly share code, notes, and snippets.

@metalmatze
Created April 6, 2017 15:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save metalmatze/2176fa76f64221563e77bd25ca0ac352 to your computer and use it in GitHub Desktop.
Save metalmatze/2176fa76f64221563e77bd25ca0ac352 to your computer and use it in GitHub Desktop.
What not to do in Go.
// Subtract deals with subtraction of all types of number.
func Subtract(left interface{}, right interface{}) interface{} {
var rleft, rright int64
var fleft, fright float64
var isInt = true
switch left.(type) {
case int:
rleft = int64(left.(int))
case int8:
rleft = int64(left.(int8))
case int16:
rleft = int64(left.(int16))
case int32:
rleft = int64(left.(int32))
case int64:
rleft = left.(int64)
case float32:
fleft = float64(left.(float32))
isInt = false
case float64:
fleft = left.(float64)
isInt = false
}
switch right.(type) {
case int:
rright = int64(right.(int))
case int8:
rright = int64(right.(int8))
case int16:
rright = int64(right.(int16))
case int32:
rright = int64(right.(int32))
case int64:
rright = right.(int64)
case float32:
fright = float64(right.(float32))
isInt = false
case float64:
fright = right.(float64)
isInt = false
}
if isInt {
return rleft - rright
}
return fleft + float64(rleft) - (fright + float64(rright))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment