What I’ve seen in the standard library was no named asserts like this, just
if checks with panics. The panic functionality does what you’ve described.
Personally I prefer the look of if+panic instead of another function for
this.
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 [email protected].
For more options, visit https://groups.google.com/d/optout.