Skip to content

Commit 4ad8245

Browse files
committed
more dynamic sample
1 parent 94cc27e commit 4ad8245

File tree

1 file changed

+24
-14
lines changed

1 file changed

+24
-14
lines changed

samples/main-play.go

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99
package main
1010

1111
import (
12+
"flag"
1213
"fmt"
1314
"io"
1415
"os"
16+
"strconv"
1517

1618
"github.com/lainio/err2"
1719
"github.com/lainio/err2/assert"
@@ -90,22 +92,23 @@ func OrgCopyFile(src, dst string) (err error) {
9092
return nil
9193
}
9294

93-
func CallRecur(d int) (err error) {
95+
func CallRecur(d int) (ret int, err error) {
9496
defer err2.Handle(&err)
9597

9698
return doRecur(d)
9799
}
98100

99-
func doRecur(d int) (err error) {
101+
func doRecur(d int) (ret int, err error) {
100102
d--
101103
if d >= 0 {
102104
// Keep below to show how asserts work
103-
assert.NotZero(d)
105+
//assert.NotZero(d)
104106
// Comment out the above assert statement to simulate runtime-error
105-
fmt.Println(10 / d)
106-
return doRecur(d)
107+
ret = 10 / d
108+
fmt.Println(ret)
109+
//return doRecur(d)
107110
}
108-
return fmt.Errorf("root error")
111+
return ret, fmt.Errorf("root error")
109112
}
110113

111114
func doPlayMain() {
@@ -136,7 +139,7 @@ func doPlayMain() {
136139
doDoMain()
137140
//try.To(doMain())
138141

139-
println("___ happy ending ===")
142+
fmt.Println("___ happy ending ===")
140143
}
141144

142145
func doDoMain() {
@@ -163,13 +166,20 @@ func doMain() (err error) {
163166
// Both source and destination don't exist
164167
//try.To(OrgCopyFile("/notfound/path/file.go", "/notfound/path/file.bak"))
165168

166-
// 2nd argument is empty
167-
try.To(OrgCopyFile("main.go", ""))
168-
169-
// Next fn demonstrates how error and panic traces work, comment out all
170-
// above CopyFile calls to play with:
171-
try.To(CallRecur(1))
169+
// to play with real args:
170+
try.To(CopyFile(flag.Arg(0), flag.Arg(1)))
171+
172+
if len(flag.Args()) > 0 {
173+
// Next fn demonstrates how error and panic traces work, comment out all
174+
// above CopyFile calls to play with:
175+
argument := try.To1(strconv.Atoi(flag.Arg(0)))
176+
ret := try.To1(CallRecur(argument))
177+
fmt.Println("ret val:", ret)
178+
} else {
179+
// 2nd argument is empty to assert
180+
try.To(OrgCopyFile("main.go", ""))
181+
}
172182

173-
println("=== you cannot see this ===")
183+
fmt.Println("=== you cannot see this ===")
174184
return nil
175185
}

0 commit comments

Comments
 (0)