Last active
July 19, 2020 23:00
-
-
Save kjk/0309cb6b537361d2aaf4eed3220e2825 to your computer and use it in GitHub Desktop.
Example for https://essential-go.programming-books.io/functions-6464727799a04611a891be7ca4877f8c (made with https://codeeval.dev)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// :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