Quick actions

cmd+k|ctrl+k

Navigation

Languages

Essential Go / Functions

Snippet info

Language

Go

Visibility

public

Author

kkowalczyk

Created

2019-03-26T10:10:11Z

Updated

2019-03-26T10:10:11Z

package main

import (
	"fmt"
)

// addCheckOverflow adds two int16 numbers and additionally
// returns true if the result overflowed
func addCheckOverflow(a, b uint16) (uint16, bool) {
	res := a + b
	overflowed := res < a || res < b
	return res, overflowed
}

func main() {
	res, overflowed := addCheckOverflow(1, 3)
	fmt.Printf("%5d + %5d = %5d, overflowed: %v\n\n", 1, 3, res, overflowed)
	res, overflowed = addCheckOverflow(65520, 10000)
	fmt.Printf("%5d + %5d = %5d, overflowed: %v\n", 65550, 10000, res, overflowed)
}
INFO