common/util.go

26 lines
454 B
Go
Raw Normal View History

2022-01-01 21:39:27 +00:00
package common
import (
2022-01-01 22:01:21 +00:00
"fmt"
"io"
2022-01-01 21:39:27 +00:00
"github.com/rs/zerolog/log"
)
// Fprint is fmt.Fprint with error handling.
func Fprint(w io.Writer, s string) {
_, err := fmt.Fprint(w, s)
if err != nil {
log.Error().Str("data", s).Err(err).Msg("Fprint failed!")
}
}
2022-01-01 22:01:21 +00:00
// Abs will give you the positive version of a negative integer, quickly.
2022-01-01 21:39:27 +00:00
func Abs(n int) int {
// ayyee smash 6ros
n64 := int64(n)
y := n64 >> 63
n64 = (n64 ^ y) - y
return int(n64)
}