Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to concat a string and float64 in Go?

Tags:

go

I am trying to solve this: http://tour.golang.org/#58

Here is what I have done:

#imports omitted
type ErrNegativeSqrt float64

func (e ErrNegativeSqrt) Error() string {
    return "Cannot Sqrt negative number: " + string(e)
}

func Sqrt(f float64) (float64, error) {
    if f < 0 {
        return 0, ErrNegativeSqrt(1)
    }
    # calculate z here...
    return z, nil
}
# main omitted

I have also tried e.String() and e.string() but those didn't work too.

like image 681
yasar Avatar asked Nov 04 '25 17:11

yasar


1 Answers

Try using the fmt package

import "fmt"
...
return fmt.Sprint("Cannot Sqrt negative number ", float64(e))
like image 177
ANisus Avatar answered Nov 07 '25 09:11

ANisus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!