es查詢-統計總數不準?
阿新 • • 發佈:2022-03-06
len
// The len built-in function returns the length of v, according to its type: // Array: the number of elements in v. // Pointer to array: the number of elements in *v (even if v is nil). // Slice, or map: the number of elements in v; if v is nil, len(v) is zero. // String: the number of bytes in v. // Channel: the number of elements queued (unread) in the channel buffer; // if v is nil, len(v) is zero. // For some arguments, such as a string literal or a simple array expression, the // result can be a constant. See the Go language specification's "Length and // capacity" section for details. func len(v Type) int
- 獲取陣列的長度,也可以通過陣列的指標來獲取長度,哪怕指標是
nil
也可以獲取,因為長度資訊其實是寫死在型別裡面的。
package main
import "fmt"
func main() {
var arr [10]int
ptr := &arr
var nilPtr *[10]int
// 輸出 10 10 10
fmt.Println(len(arr), len(ptr), len(nilPtr))
}
- 獲取切片或
map
的元素個數,如果是nil
那麼值為0。
package main import "fmt" func main() { bs := []byte{1, 2, 3} m := map[string]string{ "1": "1", "2": "2", } // 輸出 3 2 fmt.Println(len(bs), len(m)) }
- 獲取
string
字串中位元組數。
package main
import "fmt"
func main() {
// 輸出 5
fmt.Println(len("hello"))
}
- 獲取通道中快取的元素個數,如果是
nil
那麼值為0。
package main
import "fmt"
func main() {
ch1 := make(chan int, 10)
var ch2 chan int
ch1 <- 10
// 輸出 1 0
fmt.Println(len(ch1), len(ch2))
}
cap
// The cap built-in function returns the capacity of v, according to its type: // Array: the number of elements in v (same as len(v)). // Pointer to array: the number of elements in *v (same as len(v)). // Slice: the maximum length the slice can reach when resliced; // if v is nil, cap(v) is zero. // Channel: the channel buffer capacity, in units of elements; // if v is nil, cap(v) is zero. // For some arguments, such as a simple array expression, the result can be a // constant. See the Go language specification's "Length and capacity" section for // details. func cap(v Type) int
- 獲取陣列的容量,陣列的容量其實就等於長度。
package main
import "fmt"
func main() {
var arr [10]int
ptr := &arr
var nilPtr *[10]int
// 輸出 10 10 10
fmt.Println(cap(arr), cap(ptr), cap(nilPtr))
}
- 獲取切片所使用的底層陣列的容量,
map
沒有容量這個概念,所以無法使用該函式。
package main
import "fmt"
func main() {
bs := []byte{1, 2, 3}
// 輸出 3
fmt.Println(cap(bs))
}
- 獲取通道的容量。
package main
import "fmt"
func main() {
ch1 := make(chan int, 10)
var ch2 chan int
// 輸出 10 0
fmt.Println(cap(ch1), cap(ch2))
}
make
// The make built-in function allocates and initializes an object of type
// slice, map, or chan (only). Like new, the first argument is a type, not a
// value. Unlike new, make's return type is the same as the type of its
// argument, not a pointer to it. The specification of the result depends on
// the type:
// Slice: The size specifies the length. The capacity of the slice is
// equal to its length. A second integer argument may be provided to
// specify a different capacity; it must be no smaller than the
// length. For example, make([]int, 0, 10) allocates an underlying array
// of size 10 and returns a slice of length 0 and capacity 10 that is
// backed by this underlying array.
// Map: An empty map is allocated with enough space to hold the
// specified number of elements. The size may be omitted, in which case
// a small starting size is allocated.
// Channel: The channel's buffer is initialized with the specified
// buffer capacity. If zero, or the size is omitted, the channel is
// unbuffered.
func make(t Type, size ...IntegerType) Type
- 建立切片,有兩種呼叫方式:1.指定長度
make([]T, length)
,2.同時指定長度和容量make([]T, length, capacity)
。
package main
import "fmt"
func main() {
bs1 := make([]byte, 10)
bs2 := make([]byte, 10, 20)
fmt.Println(len(bs1), cap(bs1)) // 輸出 10 10
fmt.Println(len(bs2), cap(bs2)) // 輸出 10 20
}
- 建立
map
,可以為map
預先留足夠的空間去裝載指定個數的元素。
package main
import "fmt"
func main() {
m := make(map[string]string, 10)
// 輸出 0
fmt.Println(len(m))
}
- 建立通道,可以指定緩衝區大小。
package main
import "fmt"
func main() {
ch := make(chan int, 10)
// 輸出 0 10
fmt.Println(len(ch), cap(ch))
}
append
// The append built-in function appends elements to the end of a slice. If
// it has sufficient capacity, the destination is resliced to accommodate the
// new elements. If it does not, a new underlying array will be allocated.
// Append returns the updated slice. It is therefore necessary to store the
// result of append, often in the variable holding the slice itself:
// slice = append(slice, elem1, elem2)
// slice = append(slice, anotherSlice...)
// As a special case, it is legal to append a string to a byte slice, like this:
// slice = append([]byte("hello "), "world"...)
func append(slice []Type, elems ...Type) []Type
- 追加資料到切片中,會建立一個新的切片,不影響以前的切片。如果底層陣列容量不足那麼自動進行記憶體分配和拷貝。
package main
import "fmt"
func main() {
bs := make([]byte, 0)
bs2 := append(bs, []byte{1, 2, 3}...)
fmt.Println(len(bs), cap(bs)) // 輸出 0 0
fmt.Println(len(bs2), cap(bs2)) // 輸出 3 ?(容量具體多少根據底層擴容策略決定)
}
copy
// The copy built-in function copies elements from a source slice into a
// destination slice. (As a special case, it also will copy bytes from a
// string to a slice of bytes.) The source and destination may overlap. Copy
// returns the number of elements copied, which will be the minimum of
// len(src) and len(dst).
func copy(dst, src []Type) int
- 將資料從源切片拷貝到目的切片,拷貝的數量為
min(len(dst), len(src))
。
package main
import "fmt"
func main() {
bs1 := []byte{1, 2, 3, 4, 5, 6}
bs2 := []byte{4, 5, 6}
copy(bs1, bs2)
fmt.Println(bs1) // 輸出 4 5 6 4 5 6
fmt.Println(bs2) // 輸出 4 5 6
}