Skip to content

Commit

Permalink
slice
Browse files Browse the repository at this point in the history
  • Loading branch information
xujihui1985 committed Sep 17, 2018
1 parent fc2adf6 commit 8dcebb0
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
27 changes: 27 additions & 0 deletions slice/append/main.go
Original file line number Diff line number Diff line change
@@ -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])
}
17 changes: 17 additions & 0 deletions slice/rune/main.go
Original file line number Diff line number Diff line change
@@ -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])
}

}

0 comments on commit 8dcebb0

Please sign in to comment.