News for hackers. Stuff that matters.
Re:Twelve Go Best Practices by lmm on Wednesday July 31, 2013 @05:08PM (#6136853)
It's trivial to write a function to ignore exceptions if that's what you want.

    def ignoreExceptions[A](a: => A): Unit = try {a} catch {case _ =>}

    def dumpBinary(g: Gopher, w: Writer) = {
      ignoreExceptions binary.Write(w, binary.LittleEndian,     int32(len(g.Name)))
      ignoreExceptions w.Write([]byte(g.Name))
      ignoreExceptions binary.Write(w, binary.LittleEndian, g.Age)
      ignoreExceptions binary.Write(w, binary.LittleEndian, g.FurColor)
    }
Though honestly I think a better solution is monads.

    //returns Validation - either success, or the first error (which stops processing)
    //return values are directly accessible, because later code won't run unless earlier code succeeds
    for {
      _ <- binary.Write(w, binary.LittleEndian, int32(len(g.Name)))
      _ <- w.Write([]byte(g.Name))
      _ <- binary.Write(w, binary.LittleEndian, g.Age)
      _ <- binary.Write(w, binary.LittleEndian, g.FurColor)
    } yield {}
    //Runs all the operations, ignoring errors
    //type system will force you to check a return value before you can use it
    binary.Write(w, binary.LittleEndian, int32(len(g.Name)))
    w.Write([]byte(g.Name))
    binary.Write(w, binary.LittleEndian, g.Age)
    binary.Write(w, binary.LittleEndian, g.FurColor)

There are no comments on this comment yet.