Skip to content

Commit a57c475

Browse files
committed
mplayer: remove elem from slice
1 parent f0439c9 commit a57c475

File tree

10 files changed

+36
-15
lines changed

10 files changed

+36
-15
lines changed

chapter1/slice.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func remove(arr []int, i int) []int {
6+
return append(arr[:i], arr[i+1:]...)
7+
}
8+
9+
func testRemove() {
10+
a := []int{1, 2, 3, 4, 5}
11+
12+
a = remove(a, 0)
13+
fmt.Println(a) // [2, 3, 4, 5]
14+
15+
a = remove(a, 3)
16+
fmt.Println(a) // [2, 3, 4]
17+
18+
a = remove(a, 1)
19+
fmt.Println(a) // [2, 4]
20+
}
21+
22+
func main() {
23+
testRemove()
24+
}
25+

chapter3/Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
all: mplayer.build
2+
3+
mplayer.build:
4+
cd mplayer; go install -v ./...
5+

chapter3/mplayer/src/cmd/mplayer.go renamed to chapter3/mplayer/src/mplayer/mplayer.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ import (
66
"os"
77
"strconv"
88
"strings"
9-
10-
"pkg/smp/mlib"
11-
"pkg/smp/mp"
9+
"smp/mlib"
10+
"smp/mp"
1211
)
1312

1413
var lib *mlib.MusicManager

chapter3/mplayer/src/pkg/smp/mlib/manager.go renamed to chapter3/mplayer/src/smp/mlib/manager.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,7 @@ func (m *MusicManager) Remove(index int) *MusicEntry {
4545
}
4646

4747
removedMusic := &m.musics[index]
48-
49-
// Remove the found item from the slice.
50-
if index > 0 && index < len(m.musics)-1 { // Element between first and last
51-
m.musics = append(m.musics[:index], m.musics[index+1:]...)
52-
} else if index == 0 { // empty it.
53-
m.musics = m.musics[1:]
54-
} else { // The last element
55-
m.musics = m.musics[:index]
56-
}
57-
48+
m.musics = append(m.musics[:index], m.musics[index+1:]...)
5849
return removedMusic
5950
}
6051

@@ -70,3 +61,4 @@ func (m *MusicManager) RemoveByName(name string) *MusicEntry {
7061
}
7162
return nil
7263
}
64+

env.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
export GOPATH=$GOPATH:$GOBOOK/chapter1/calcproj:$GOBOOK/chapter2/sorter:$GOBOOK/chapter4/cgss
2-
export PATH=$PATH:$GOBOOK/chapter1/calcproj/bin:$GOBOOK/chapter2/sorter/bin:$GOBOOK/chapter4/cgss/bin
1+
export GOPATH=$GOPATH:$GOBOOK/chapter1/calcproj:$GOBOOK/chapter2/sorter:$GOBOOK/chapter3/mplayer:$GOBOOK/chapter4/cgss
2+
export PATH=$PATH:$GOBOOK/chapter1/calcproj/bin:$GOBOOK/chapter2/sorter/bin:$GOBOOK/chapter3/mplayer/bin:$GOBOOK/chapter4/cgss/bin
33

0 commit comments

Comments
 (0)