Skip to content

Instantly share code, notes, and snippets.

@jswank
Created December 6, 2011 16:34
Show Gist options
  • Save jswank/1438851 to your computer and use it in GitHub Desktop.
Save jswank/1438851 to your computer and use it in GitHub Desktop.
Example Code
package main
import (
"fmt"
"os"
)
func foo(i int) int {
throw_if (i == 42,"foo: 42 not supported")
return 2*i
}
func bar(s string) string {
throw_if (s == "baz","bar: 'baz' not supported")
return s + s
}
func throw(msg string) {
panic(os.NewError(msg))
}
func throw_if(condn bool, msg string) {
if condn {
throw(msg)
}
}
func catch(perr *os.Error) {
if e := recover(); e != nil {
*perr = e.(os.Error)
}
}
func z() (x int, y string, err os.Error) {
defer catch (&err)
x = foo(2)
y = bar("")
y += bar("baz")
x += foo(42)
return
}
func main() {
fmt.Println(z()...)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment