I may have misunderstood the question. I follow the idea of panic when the 
program is in an invalid state.

If Divide can receive any input then this is probably a better API:

func Divide(a, b float64) (float64, error) {

where you would return an ErrDivideByZero made with errors.New as a global 
exported var instead of panicking.

But if Divide can only receive valid input then that assert seems 
appropriate to me.

Matt

On Monday, May 14, 2018 at 7:38:32 PM UTC-5, Tristan Muntsinger wrote:
>
> Is it reasonable to use a function like "Assert" below to do validation 
> checking in Go?  If not, why not?
>
> func Assert(t bool, msg string) {
> if !t {
> debug.SetTraceback("all")
> debug.PrintStack()
> log.Fatal("Assertion failed: " + msg)
> }
> }
>
> func Divide(a float64, b float64) float64 {
> Assert(b != 0, "divide by 0")
> return a / b
> }
>
> func main() {
> fmt.Println(Divide(10, 5))
> }
>
> Thanks,
> -Tristan
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to