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{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])
}
时间: 2024-12-24 18:39:00

A Tour of Go Slicing slices的相关文章

A Tour of Go Nil slices

The zero value of a slice is nil. A nil slice has a length and capacity of 0. (To learn more about slices, read the Slices: usage and internalsarticle.) package main import "fmt" func main() { var z []int fmt.Println(z, len(z), cap(z)) if z == n

A Tour of Go Making slices

Slices are created with the make function. It works by allocating a zeroed array and returning a slice that refers to that array: a := make([]int, 5) // len(a)=5 To specify a capacity, pass a third argument to make: b := make([]int, 0, 5) // len(b)=0

A Tour of Go Exercise: Slices

Implement Pic. It should return a slice of length dy, each element of which is a slice of dx 8-bit unsigned integers. When you run the program, it will display your picture, interpreting the integers as grayscale (well, bluescale) values. The choice

Exercise: Slices (画图)

A Tour of Go Exercise: Slices https://tour.golang.org/moretypes/18 这道题目,提供了一个画图函数 (pic.Show), 可以生成图片. 这个函数,即 pic.Show(f func(int, int) [][]uint8), 可见,它接受一个函数做参数,题目要求的正是编写这个参数.答案如下: 这里面,依赖一个 package, 即 "golang.org/x/tour/pic" 我上 https://github.co

A Tour of Go Slices

A slice points to an array of values and also includes a length. []T is a slice with elements of type T. package main import "fmt" func main() { p := []int{2, 3, 5, 7, 11, 13} fmt.Println("p ==", p) for i := 0; i < len(p); i++ { fmt

Go Slices: usage and internals

Introduction Go's slice type provides a convenient and efficient means of working with sequences of typed data. Slices are analogous to arrays in other languages, but have some unusual properties. This article will look at what slices are and how the

[label][Fireworks][转载] Web Slices - Fireworks CS5

Web Slices – Fireworks CS5 http://bestwebdesignz.com/tips/fireworks/web-slices-fireworks-cs5/ Need a Website? Contact Us Now! This entry is part 12 of 14 in the series Fireworks CS5 Tutorial It is time to slice up the layout for the web now that it i

Python学习--字符串slicing

Found this great table at http://wiki.python.org/moin/MovingToPythonFromOtherLanguages Python indexes and slices for a six-element list. Indexes enumerate the elements, slices enumerate the spaces between the elements. Index from rear: -6 -5 -4 -3 -2

Go structs、slices、maps

[Go structs.slices.maps] 1.定义时*在变量名后面,使用时*在变量名前面. 2.定义struct,type在前,struct关键字在后. 3.指针可以指定struct. 4.A struct literal denotes a newly allocated struct value by listing the values of its fields. You can list just a subset of fields by using the Name: sy