From 7a12625ff59ffb32f96b7cef361038e36be1c55a Mon Sep 17 00:00:00 2001 From: TalonsLee Date: Sat, 1 Feb 2020 22:42:01 +0800 Subject: [PATCH] =?UTF-8?q?Revert=20"=E4=BF=AE=E5=A4=8D=E4=BB=A3=E7=A0=81b?= =?UTF-8?q?ug-->=20exercises/chapter=5F7/remove=5Fslice.go=20(#433)"=20(#7?= =?UTF-8?q?57)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Changes to these two files seem not necessary. This reverts commit 695188fe0d38df0b8d04ada5b589e8c74a39883f. --- eBook/exercises/chapter_7/remove_slice.go | 4 ++-- eBook/exercises/chapter_7/split_string.go | 27 ++++++++++------------- 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/eBook/exercises/chapter_7/remove_slice.go b/eBook/exercises/chapter_7/remove_slice.go index 0749c26..7e3724d 100755 --- a/eBook/exercises/chapter_7/remove_slice.go +++ b/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 } diff --git a/eBook/exercises/chapter_7/split_string.go b/eBook/exercises/chapter_7/split_string.go index d7d29fa..c4a8c04 100755 --- a/eBook/exercises/chapter_7/split_string.go +++ b/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:] +}