码迷,mamicode.com
首页 > 其他好文 > 详细

A Tour of Go Slicing slices

时间:2014-10-27 01:46:25      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   ar   sp   div   on   log   

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

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{2, 3, 5, 7, 11, 13}
    fmt.Println("p ==", p)
    fmt.Println("p[1:4] ==", p[1:4])

    //missing low index implies 0
    fmt.Println("p[:3] ==", p[:3])

    // missing high index implies len(s)
    fmt.Println("p[4:] ==", p[4:])
}

 

package main 

import "fmt"

func main() {
    p := []int{2, 3, 5, 7, 11, 13}
    fmt.Println("p ==", p)
    fmt.Println("p[1:4] ==", p[1:4])

    //missing low index implies 0
    fmt.Println("p[:3] ==", p[:3])

    // missing high index implies len(s)
    fmt.Println("p[4:] ==", p[4:])
    var a [2]string
    a[0] = "Hello"
    a[1] = "World"
    fmt.Println(a[0:1])
}

 

A Tour of Go Slicing slices

标签:style   blog   color   io   ar   sp   div   on   log   

原文地址:http://www.cnblogs.com/ghgyj/p/4053317.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!