Created
December 6, 2011 16:34
-
-
Save jswank/1438851 to your computer and use it in GitHub Desktop.
Example Code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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