Jelajahi Sumber

Revert "修复代码bug--> exercises/chapter_7/remove_slice.go (#433)" (#757)

Changes to these two files seem not necessary.

This reverts commit 695188fe0d38df0b8d04ada5b589e8c74a39883f.
TalonsLee 6 tahun lalu
induk
melakukan
7a12625ff5

+ 2 - 2
eBook/exercises/chapter_7/remove_slice.go

@@ -12,8 +12,8 @@ func main() {
 }
 
 func RemoveStringSlice(slice []string, start, end int) []string {
-	result := make([]string, len(slice)-(end-start)-1)
+	result := make([]string, len(slice)-(end-start))
 	at := copy(result, slice[:start])
-	copy(result[at:], slice[end+1:])
+	copy(result[at:], slice[end:])
 	return result
 }

+ 12 - 15
eBook/exercises/chapter_7/split_string.go

@@ -1,19 +1,16 @@
 package main
 
-import (
-        "fmt"
-    )
+import "fmt"
 
-    func main() {
-            rawString := "Google"
-                index := 3
-                    sp1, sp2 := splitStringbyIndex(rawString, index)
-                        fmt.Printf("The string %s split at position %d is: %s / %s\n", rawString, index, sp1, sp2)
-                    }
+func main() {
+	str := "Google"
+	for i := 0; i <= len(str); i++ {
+		a, b := Split(str, i)
+		fmt.Printf("The string %s split at position %d is: %s / %s\n", str, i, a, b)
+	}
 
-                    func splitStringbyIndex(str string, i int) (sp1, sp2 string) {
-                            rawStrSlice := []byte(str)
-                                sp1 = string(rawStrSlice[:i])
-                                    sp2 = string(rawStrSlice[i:])
-                                        return
-                                    }
+}
+
+func Split(s string, pos int) (string, string) {
+	return s[0:pos], s[pos:]
+}