Skip to content

Commit 40cb68c

Browse files
authored
Merge pull request #99 from devlights/add-reverse-func
Add slice reverse example
2 parents 08e7b3e + 0e2f444 commit 40cb68c

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

basic/slice_/slice_reverse.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package slice_
2+
3+
import "fmt"
4+
5+
func reverseInts(s []int) {
6+
7+
var (
8+
first = 0
9+
last = len(s) - 1
10+
)
11+
12+
for first < last {
13+
s[first], s[last] = s[last], s[first]
14+
first++
15+
last--
16+
}
17+
}
18+
19+
func reverseStrs(s []string) {
20+
21+
var (
22+
first = 0
23+
last = len(s) - 1
24+
)
25+
26+
for first < last {
27+
s[first], s[last] = s[last], s[first]
28+
first++
29+
last--
30+
}
31+
}
32+
33+
func SliceReverse() error {
34+
35+
var (
36+
ints = []int{
37+
1, 2, 3, 4, 5,
38+
}
39+
40+
strs = []string{
41+
"hello", "world",
42+
}
43+
44+
f = func(i []int, s []string) {
45+
fmt.Printf("[original]\tints[%v]\tstrs[%v]\n", ints, strs)
46+
}
47+
)
48+
49+
f(ints, strs)
50+
51+
reverseInts(ints)
52+
reverseStrs(strs)
53+
54+
f(ints, strs)
55+
56+
return nil
57+
}

lib/mapping.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ func (m SampleMapping) MakeMapping() {
6868
m["slice03"] = slice_.Slice03
6969
m["slice04"] = slice_.Slice04
7070
m["slice05"] = slice_.Slice05
71+
m["slice_reverse"] = slice_.SliceReverse
7172
m["comment01"] = comments.Comment01
7273
m["closure01"] = closure.Closure01
7374
m["string01"] = string_.String01

0 commit comments

Comments
 (0)