Skip to content

Instantly share code, notes, and snippets.

@uzilan
Last active October 21, 2019 07:03
Show Gist options
  • Save uzilan/f9c254fbf504843e3b763622a255fe2e to your computer and use it in GitHub Desktop.
Save uzilan/f9c254fbf504843e3b763622a255fe2e to your computer and use it in GitHub Desktop.
gosoup
func GetANumber() int {
rand.Seed(time.Now().UnixNano()) // different seed every time
return rand.Intn(100) // generate a random number
}
func BenchmarkGetANumber(b *testing.B) {
for i := 0; i < b.N; i++ {
GetANumber()
}
}
func Divide(x float32, y float32) (float32, error) {
if y == 0 {
return 0, errors.New("division by zero") // an error occurred
}
return x / y, nil // result and no error
}
func DivisionRunner(x float32, y float32) {
result, err := Divide(x, y)
if err == nil {
fmt.Printf("%v / %v = %v", x, y, result)
} else {
fmt.Printf("error: %v", err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment