append 的第一个参数 s 是一个元素类型为 T 的切片, 其余类型为 T 的值将会追加到该切片的末尾。
append 返回值: 一个包含原切片所有元素加上新添加元素的切片。
当 s 的底层数组太小,不足以容纳所有给定的值时,它就会分配一个更大的数组。 返回的切片会指向这个新分配的数组。
func make([]T, len, cap) []T
切片可以使用内置函数 make 创建;
参数: []T: T 为切片中元素的类型;
len : 该切片的长度;
cap : 该切片的容量; 当参数为 2个时, 第二个参数即为长度,也是容量. 即len == cap
返回值: 为该数组对应的切片;
使用内置函数 len 和 cap 获取切片的长度和容量信息。
例如: s := make([]byte, 5) len(s) == 5 cap(s) == 5
func copy(dst, src []T) int
copy 函数将源切片的元素复制到目的切片。 它返回复制元素的数目。
copy 函数支持不同长度的切片之间的复制(它只复制较短切片的长度个元素), 所以保证 src的容量 <= dst的容量.
此外, copy 函数可以正确处理源和目的切片有重叠的情况。
例如: func AppendByte(slice []byte, data ...byte) []byte { m := len(slice) n := m + len(data) if n > cap(slice) { // if necessary, reallocate // allocate double what's needed, for future growth. newSlice := make([]byte, (n+1)*2) copy(newSlice, slice) slice = newSlice } slice = slice[0:n] copy(slice[m:n], data) fmt.Println(slice) return slice }
func append(s []T, x ...T) []T
append 函数将 x 追加到切片 s 的末尾,并且在必要的时候增加容量。
如果是要将一个切片追加到另一个切片尾部,需要使用 ... 语法将第2个参数展开为参数列表。
例如: a := []string{"John", "Paul"} b := []string{"George", "Ringo", "Pete"} a = append(a, b...) // equivalent to "append(a, b[0], b[1], b[2])" // a == []string{"John", "Paul", "George", "Ringo", "Pete"}
Range
for 循环的 range 形式可遍历切片或映射
当使用 for 循环遍历切片时,每次迭代都会返回两个值。 第一个值为当前元素的下标,第二个值为该下标所对应元素的一份副本.
可以将下标或值赋予 _ 来忽略它
若你只需要索引,去掉 , value 的部分即可
例如: func main() { var result = []int{1, 2, 4, 8, 16, 32, 64, 128} for i, v := range result { fmt.Printf("i = %d, value = %d\n", i, v) } for i:= range result { fmt.Printf("i = %d\n", i) } } 输出: i = 0, value = 1 i = 1, value = 2 i = 2, value = 4 i = 3, value = 8 i = 4, value = 16 i = 5, value = 32 i = 6, value = 64 i = 7, value = 128 i = 0 i = 1 i = 2 i = 3 i = 4 i = 5 i = 6 i = 7
当使用 for 循环遍历 map结构(即映射)时, 返回两个值:第一个值为 key, 第二个值为key对应的value.
当map结构中无元素时, 该循环不会进入.直接跳过.
func ReadFile(filename string) ([]byte, error)
ReadFile reads the file named by filename and returns the contents. A successful call returns err == nil, not err == EOF. Because ReadFile reads the whole file, it does not treat an EOF from Read as an error to be reported.
ResolveTCPAddr parses addr as a TCP address of the form "host:port" or "[ipv6-host%zone]:port" and resolves a pair of domain name and port name on the network net, which must be "tcp", "tcp4" or "tcp6". A literal address or host name for IPv6 must be enclosed in square brackets, as in "[::1]:80", "[ipv6-host]:http" or "[ipv6-host%zone]:80".
Resolving a hostname is not recommended because this returns at most one of its IP addresses.
LocalAddr returns the local network address. The Addr returned is shared by all invocations of LocalAddr, so do not modify it.
翻译: 该函数返回一个本地的网路地址. 这个调用函数返回的地址被所有调用共享,不要去修改它..
func (c *IPConn) RemoteAddr() Addr
RemoteAddr returns the remote network address. The Addr returned is shared by all invocations of RemoteAddr, so do not modify it.
翻译: 该函数返回一个 远程的网络地址. 该掉用函数返回的地址 被整台机器共享, 不要去修改它.
func Dial(network, address string) (Conn, error)
Dial connects to the address on the named network.
Known networks are "tcp", "tcp4" (IPv4-only), "tcp6" (IPv6-only), "udp", "udp4" (IPv4-only), "udp6" (IPv6-only), "ip", "ip4" (IPv4-only), "ip6" (IPv6-only), "unix", "unixgram" and "unixpacket".
For TCP and UDP networks, addresses have the form host:port. If host is a literal IPv6 address it must be enclosed in square brackets as in "[::1]:80" or "[ipv6-host%zone]:80". The functions JoinHostPort and SplitHostPort manipulate addresses in this form. If the host is empty, as in ":80", the local system is assumed.
func Caller(skip int) (pc uintptr, file string, line int, ok bool)
Caller reports file and line number information about function invocations on the calling goroutine's stack. The argument skip is the number of stack frames to ascend, with 0 identifying the caller of Caller. (For historical reasons the meaning of skip differs between Caller and Callers.) The return values report the program counter, file name, and line number within the file of the corresponding call. The boolean ok is false if it was not possible to recover the information.
翻译:返回调用函数的行号和文件名
func Exit(code int)
Exit causes the current program to exit with the given status code. Conventionally, code zero indicates success, non-zero an error. The program terminates immediately; deferred functions are not run.