Skip to content

Instantly share code, notes, and snippets.

@kjk
Last active July 19, 2020 23:00
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 kjk/0309cb6b537361d2aaf4eed3220e2825 to your computer and use it in GitHub Desktop.
Save kjk/0309cb6b537361d2aaf4eed3220e2825 to your computer and use it in GitHub Desktop.
// :collection Essential Go
package main
import (
"fmt"
)
// :show start
// addCheckOverflow adds two int16 numbers and additionally
// returns true if the result overflowed
func addCheckOverflow(a, b uint16) (uint16, bool) {
res := a + b
didOverflow := res < a || res < b
return res, didOverflow
}
func main() {
res, didOverflow := addCheckOverflow(1, 3)
fmt.Printf("%5d + %5d = %5d, did overflow?: %v\n\n", 1, 3, res, didOverflow)
res, didOverflow = addCheckOverflow(65520, 10000)
fmt.Printf("%5d + %5d = %5d, did overflow?: %v\n", 65550, 10000, res, didOverflow)
}
// :show end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment