***數組
func Join(a []string, sep string) string服務器
- Join concatenates the elements of a to create a single string. The separator string sep is placed between elements in the resulting string.
- 翻譯: 根據切片a中的元素生成一個字符串, sep分割字符串被放置在生成的字符串中間.
例如:
s := []string{"hah", "nihao", "wode"} fmt.Println(strings.Join(s, ",, ")) fmt.Println(strings.Join(s, "")) 輸出: hah,, nihao,, wode hahnihaowode
func append(s []T, vs …T) []T網絡
- append 的第一個參數 s 是一個元素類型爲 T 的切片, 其他類型爲 T 的值將會追加到該切片的末尾。
- append 返回值: 一個包含原切片全部元素加上新添加元素的切片。
- 當 s 的底層數組過小,不足以容納全部給定的值時,它就會分配一個更大的數組。 返回的切片會指向這個新分配的數組。
func make([]T, len, cap) []Tapp
- 切片可使用內置函數 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) intdom
- 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) []Tsocket
- 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"}
Rangetcp
- 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)ide
- 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.
- 翻譯: 讀取filename 文件, 返回出文件文件所有內容. 成功: err == nil; 失敗: err == EOF;
注意: 由於ReadFile() 讀取的是整個文件, 因此在正常讀取過程當中, 文件末尾的EOF並不會當作錯誤進行報告.
func ResolveTCPAddr(net, addr string) (*TCPAddr, error)函數
- 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.- 翻譯: 該函數將參數 addr解析爲TCP格式的地址塊, 而且根據net參數解析一對域名和端口, net必須爲: TCP..., 自滿地址或主機名爲IPV6的解析結果必須包含在括號中. 解析一個主機名不推薦使用該函數,由於它最多返回一個IP地址.
func (c *IPConn) Read(b []byte) (int, error)學習
- Read implements the Conn Read method.
- 翻譯:該函數實現了從套接字中讀取數據, 返回讀取的長度和出錯信息. 讀取的內容拷貝在參數b中. 當該套接字關閉或出錯, err被賦值錯誤信息返回. 接收成功, err爲nil.
func (c *TCPConn) LocalAddr() Addr
- 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.- 翻譯: 該函數依據 network 連接address指向的地址.
network標誌有...
對於 TCP,UDP標識, address格式爲host:port, 若是host爲IPV6, 則必須被包含在方括號中. 函數JoinHostPort和SplitHostPort控制host格式. 若是host爲空, 默認爲本機.
注意: 1.當network == TCP 時, client連接address地址, 服務器未監聽時, 該函數調用會當即報錯.由於 TCP協議的server端, 有socket(), bind(), listen(), accept(), 隨後纔會在連接上的套接字接收信息.
注意: 2.當network == UDP 時, client連接address地址, 即便服務器未開啓, 該函數也會成功.由於 UDP協議的server端, 只有socket(), bind(), 而後即在該套接字端口接收來自客戶端的信息
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.
- 翻譯: exit函數使當前程序退出,code爲退出狀態. 同常: 0標識正常退出, 非0 標識錯誤.. 做用爲: 程序當即終止, 而且,而且,注意:defer 關鍵字中的函數不會被執行.
func