-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1.gop
48 lines (35 loc) · 1.06 KB
/
1.gop
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import (
"strconv"
)
func add(x, y string) (int, error) {
return strconv.atoi(x)? + strconv.atoi(y)?, nil
}
func addSafe(x, y string) int {
return strconv.atoi(x)?:0 + strconv.atoi(y)?:0
}
// Error handling
// We reinvent the error handling specification in Go+. We call them ErrWrap expressions:
// expr! // panic if err
// expr? // return if err
// expr?:defval // use defval if err
println `add("100", "23"):`, add("100", "23")!
sum, err := add("10", "abc")
println `add("10", "abc"):`, sum, err
println `addSafe("10", "abc"):`, addSafe("10", "abc")
a := [x*x for x <- [1, 3, 5, 7, 11]]
println a
b := [x*x for x <- [1, 3, 5, 7, 11], x > 3]
println b
c := [i+v for i, v <- [1, 3, 5, 7, 11], i%2 == 1]
println c
d := [k+","+s for k, s <- {"Hello": "xsw", "Hi": "Go+"}]
println d
arr := [1, 2, 3, 4, 5, 6]
e := [[a, b] for a <- arr, a < b for b <- arr, b > 2]
println e
x := {x: i for i, x <- [1, 3, 5, 7, 11]}
println x
y := {x: i for i, x <- [1, 3, 5, 7, 11], i%2 == 1}
println y
z := {v: k for k, v <- {1: "Hello", 3: "Hi", 5: "xsw", 7: "Go+"}, k > 3}
println z