From 8dcebb04fe5bcc94ffee56bcc12d90984974f607 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=85=95=E9=99=B6?= Date: Mon, 17 Sep 2018 21:13:00 +0800 Subject: [PATCH] slice --- slice/append/main.go | 27 +++++++++++++++++++++++++++ slice/rune/main.go | 17 +++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 slice/append/main.go create mode 100644 slice/rune/main.go diff --git a/slice/append/main.go b/slice/append/main.go new file mode 100644 index 0000000..b325542 --- /dev/null +++ b/slice/append/main.go @@ -0,0 +1,27 @@ +package main + +import "fmt" + +func main() { + case1() + case2() +} + +// 当capacity +func case1() { + s1 := make([]string, 1, 20) + s1[0] = "hello" + p1 := &s1[0] + s1 = append(s1, "world") + *p1 = "hello2" + fmt.Printf("value of p1 is %s, value of s1[0] is %s \n", *p1, s1[0]) +} + +func case2() { + s1 := make([]string, 1) + s1[0] = "hello" + p1 := &s1[0] + s1 = append(s1, "world") + *p1 = "hello2" + fmt.Printf("value of p1 is %s, value of s1[0] is %s \n", *p1, s1[0]) +} diff --git a/slice/rune/main.go b/slice/rune/main.go new file mode 100644 index 0000000..e0704ed --- /dev/null +++ b/slice/rune/main.go @@ -0,0 +1,17 @@ +package main + +import ( + "fmt" + "unicode/utf8" +) + +func main() { + s := "中文, hello world" + + for i, r := range s { + rl := utf8.RuneLen(r) + offset := i + rl + fmt.Printf("codepoint: %#6x; offset: %d, char %s\n", r, offset, s[i:i+rl]) + } + +}