Skip to content

Commit a343cfb

Browse files
authored
Merge pull request #68 from tsingbx/slices
add doc for slices
2 parents 828b9a3 + f9a2dcd commit a343cfb

16 files changed

Lines changed: 240 additions & 82 deletions

117-Slices/slices-0.gop

Lines changed: 0 additions & 53 deletions
This file was deleted.

117-Slices/slices-01.gop

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Slice literals in Go+ style
2+
// A slice is a collection of data elements of the same type. A slice literal is a list of expressions surrounded by square brackets. An individual element can be accessed using an index expression. Indexes start from 0.
3+
//
4+
// In go+, you can get slice length directly from len method, and you can casting slice literals:
5+
6+
f64 := []float64([1, 2, 3]) // []float64
7+
println(f64, f64.len) // get length by len method
8+
9+
nums := [1, 2, 3]
10+
println nums // [1 2 3]
11+
println nums.len // 3, go+ support
12+
println nums[0] // 1
13+
println nums[1:3] // [2 3]
14+
println nums[:2] // [1 2]
15+
println nums[2:] // [3]
16+
17+
nums[1] = 5
18+
println nums // [1 5 3]

117-Slices/slices-02.gop

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Slice
2+
// An array has a fixed size. A slice, on the other hand, is a dynamically-sized, flexible view into the elements of an array. In practice, slices are much more common than arrays.
3+
//
4+
// The type []T is a slice with elements of type T.
5+
//
6+
// A slice is formed by specifying two indices, a low and high bound, separated by a colon: a[low : high]. This selects a half-open range which includes the first element, but excludes the last one.

117-Slices/slices-03.gop

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Slices are like references to arrays
2+
// A slice does not store any data, it just describes a section of an underlying array.
3+
//
4+
// Changing the elements of a slice modifies the corresponding elements of its underlying array.
5+
//
6+
// Other slices that share the same underlying array will see those changes.
7+
8+
names := [4]string{
9+
"John",
10+
"Paul",
11+
"George",
12+
"Ringo",
13+
}
14+
println names
15+
16+
a := names[0:2]
17+
b := names[1:3]
18+
println a, b
19+
20+
b[0] = "XXX"
21+
println a, b
22+
println names

117-Slices/slices-04.gop

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Slice literals
2+
// A slice literal is like an array literal without the length.
3+
// <pre>
4+
// This is an array literal:
5+
// [3]bool{true, true, false}
6+
// And this creates the same array as above, then builds a slice that references it:
7+
// []bool{true, true, false}
8+
// </pre>
9+
10+
q := []int{2, 3, 5, 7, 11, 13}
11+
println q
12+
13+
r := []bool{true, false, true, true, false, true}
14+
println r
15+
16+
s := []struct {
17+
i int
18+
b bool
19+
}{
20+
{2, true},
21+
{3, false},
22+
{5, true},
23+
{7, true},
24+
{11, false},
25+
{13, true},
26+
}
27+
println s

117-Slices/slices-05.gop

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Slice defaults
2+
// When slicing, you may omit the high or low bounds to use their defaults instead.
3+
//
4+
// The default is zero for the low bound and the length of the slice for the high bound.
5+
//<pre>
6+
// For the array
7+
// var a [10]int
8+
// these slice expressions are equivalent:
9+
// a[0:10]
10+
// a[:10]
11+
// a[0:]
12+
// a[:]
13+
//</pre>
14+
s := []int{2, 3, 5, 7, 11, 13}
15+
16+
s = s[1:]
17+
println s
18+
19+
s = s[1:4]
20+
println s
21+
22+
s = s[:2]
23+
println s

117-Slices/slices-06.gop

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Slice length and capacity
2+
// A slice has both a length and a capacity.
3+
//
4+
// The length of a slice is the number of elements it contains.
5+
//
6+
// The capacity of a slice is the number of elements in the underlying array, counting from the first element in the slice.
7+
//
8+
// The length and capacity of a slice s can be obtained using the expressions len(s) and cap(s).
9+
//
10+
// You can extend a slice's length by re-slicing it, provided it has sufficient capacity. Try changing one of the slice operations in the example program to extend it beyond its capacity and see what happens.
11+
12+
func printSlice(s []int) {
13+
printf "len=%d cap=%d %v\n", len(s), cap(s), s
14+
}
15+
16+
s := []int{2, 3, 5, 7, 11, 13}
17+
printSlice s
18+
19+
s = s[:0] // Slice the slice to give it zero length.
20+
printSlice s
21+
22+
s = s[:4] // Extend its length.
23+
printSlice s
24+
25+
s = s[2:] // Drop its first two values.
26+
printSlice s

117-Slices/slices-07.gop

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Nil slices
2+
// The zero value of a slice is nil.
3+
//
4+
// A nil slice has a length and capacity of 0 and has no underlying array.
5+
6+
var s []int
7+
8+
println s, len(s), cap(s)
9+
10+
if s == nil {
11+
println "nil!"
12+
}

117-Slices/slices-08.gop

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Creating a slice with make
2+
// Slices can be created with the built-in make function; this is how you create dynamically-sized arrays.
3+
//
4+
// The make function allocates a zeroed array and returns a slice that refers to that array:
5+
//
6+
//<pre>
7+
//a := make([]int, 5) // len(a)=5
8+
//To specify a capacity, pass a third argument to make:
9+
//b := make([]int, 0, 5) // len(b)=0, cap(b)=5
10+
//b = b[:cap(b)] // len(b)=5, cap(b)=5
11+
//b = b[1:] // len(b)=4, cap(b)=4
12+
//</pre>
13+
14+
func printSlice(s string, x []int) {
15+
printf "%s len=%d cap=%d %v\n",
16+
s, len(x), cap(x), x
17+
}
18+
19+
a := make([]int, 5)
20+
printSlice "a", a
21+
22+
b := make([]int, 0, 5)
23+
printSlice "b", b
24+
25+
c := b[:2]
26+
printSlice "c", c
27+
28+
d := c[2:5]
29+
printSlice "d", d

117-Slices/slices-09.gop

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Slices of slices
2+
// Slices can contain any type, including other slices.
3+
// Create a tic-tac-toe board.
4+
5+
import (
6+
"strings"
7+
)
8+
9+
board := [][]string{
10+
[]string{"_", "_", "_"},
11+
[]string{"_", "_", "_"},
12+
[]string{"_", "_", "_"},
13+
}
14+
15+
board[0][0] = "X" // The players take turns.
16+
board[2][2] = "O"
17+
board[1][2] = "X"
18+
board[1][0] = "O"
19+
board[0][2] = "X"
20+
21+
for i := 0; i < len(board); i++ {
22+
printf "%s\n", strings.join(board[i], " ")
23+
}

0 commit comments

Comments
 (0)