A Tour of Go Slicing slices

---恢复内容开始---

Slices can be re-sliced, creating a new slice value that points to the same array.

The expression

s[lo:hi]

evaluates to a slice of the elements from lo through hi-1, inclusive. Thus

s[lo:lo]

is empty and

s[lo:lo+1]

has one element.

package main 

import "fmt"

func main() {
p := []int{, , , , , }
fmt.Println("p ==", p)
fmt.Println("p[1:4] ==", p[:]) //missing low index implies 0
fmt.Println("p[:3] ==", p[:]) // missing high index implies len(s)
fmt.Println("p[4:] ==", p[:])
}
package main 

import "fmt"

func main() {
p := []int{, , , , , }
fmt.Println("p ==", p)
fmt.Println("p[1:4] ==", p[:]) //missing low index implies 0
fmt.Println("p[:3] ==", p[:]) // missing high index implies len(s)
fmt.Println("p[4:] ==", p[:])
var a []string
a[] = "Hello"
a[] = "World"
fmt.Println(a[:])
}
上一篇:StringBuilder 比 String 快?空嘴白牙的,证据呢!


下一篇:servlet 学习笔记(二)