璋堣皥golang涓殑寮曠敤绫诲瀷涓庡湴鍧€鍒嗛厤
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了璋堣皥golang涓殑寮曠敤绫诲瀷涓庡湴鍧€鍒嗛厤相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/golang' title='golang'>golang
杈撳叆 澹版槑 update 鍒嗛厤 code 鍑芥暟瀹氫箟 element oda 璋堣皥golang涓殑寮曠敤绫诲瀷涓庡湴鍧€鍒嗛厤
go璇█涓殑寮曠敤绫诲瀷
(鎴戞洿鎰挎剰绉板叾涓?code>鎸囧悜绫诲瀷
)鍏跺疄灏辨槸瀛樻斁鍦板潃鐨勭被鍨?/code>,姣斿slice
,map
,channel
,鍜屽叾浠栨寚閽堢被鍨?*int
...)
瑕佸疄鐢ㄨ繖浜?瀛樻斁鍦板潃鐨?绫诲瀷涔嬪墠灏卞繀椤昏涓哄叾鍒嗛厤鍐呭瓨
.涓嬮潰鎴戜竴涓€涓句緥璇存槑涓€涓?浠ヤ笅鎿嶄綔鍧囧湪鍑芥暟鍐呴儴鎿嶄綔,鎰忓懗鐫€鍙互浣跨敤:=
鏉ュ0鏄庡畾涔夊彉閲?
1. 鍊肩被鍨嬬殑鎸囬拡褰㈠紡
浠nt涓轰緥
-
涓€涓敊璇殑渚嬪瓙
var i *int
*i = 1
panic: runtime error: invalid memory address or nil pointer dereference
閿欒鍘熷洜,i涓烘寚閽堝彉閲?浣嗘槸鏈鍒嗛厤鍦板潃灏辫瘯鍥炬搷浣滃叾鍐呭
-
瑙e喅鏂规硶(鍐欐硶涓€)
var i *int
i = new(int)
*i = 1
-
瑙e喅鏂规硶(鍐欐硶浜?
i := new(int)
*i = 1
-
new鍑芥暟
new鍑芥暟鐢ㄤ簬缁欐湰韬笉甯︽湁鎸囬拡绫诲瀷鐨勭被鍨嬪甫涓婃寚閽?杩欒瘽鏄垜缁欏嚭鐨勮В閲?璇寸櫧浜嗗氨鏄緭鍏?code>鍊肩被鍨?/code>杩斿洖璇ョ被鍨嬬殑鎸囬拡
.new鍑芥暟鍙互鍒嗛厤鍐呭瓨骞朵笖鍒濆鍖栦负闆跺€?杩欐槸浠栫殑鍑芥暟瀹氫箟:func new(Type) *Type
2. Slice
slice鏈韩灏辨槸涓€涓甫鎸囬拡鐨勭被鍨?寮曠敤绫诲瀷),鎵€浠ヤ娇鐢ㄤ粬涔嬪墠蹇呴』鍒嗛厤鍦板潃,浣嗕粬姣旇緝鐗规畩,鐢变簬append
鍑芥暟鐨勫姛鑳?鎴戜滑鍑犱箮涓嶄細閬囧埌slice
鍐呭瓨鏈垎閰嶇殑鍑芥暟,鏇村鐨勬槸閬囧埌涓嬫爣瓒婄晫
-
澹版槑鏃朵负鍒嗛厤鍐呭瓨鐨勪緥瀛愪竴
var s []int // 瀹氫箟涓€涓瓨鏀緄nt鐨勫垏鐗?鏈垎閰嶅唴瀛?s[0] = 1 // 鎶ラ敊,panic: runtime error: index out of range [0] with length 0
-
澹版槑鏃朵负鍒嗛厤鍐呭瓨鐨勪緥瀛愪簩
var s []int // 瀹氫箟涓€涓瓨鏀緄nt鐨勫垏鐗?鏈垎閰嶅唴瀛?s = append(s, 1) // 鏈姤閿?
鏈変簺浜哄彲鑳戒細鎰熷埌濂囨€?杩欓噷鏄庢槑娌℃湁鍒嗛厤鍐呭瓨鎬庝箞鍙互浣跨敤slice鑰屼笉鎶ラ敊,鍘熷洜灏辨槸,append鍑芥暟浼氳嚜鍔ㄥ垎閰嶅唴瀛?鍏跺疄灏辨槸鎵╁绛栫暐,娌℃湁鍙戠敓涓嬫爣瓒婄晫閿欒涔熸槸鍥犱负鎵╁绛栫暐),杩欐槸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
-
make鍑芥暟瀵逛簬slice
make鍑芥暟鏄敤浜庡slice
,map
,channell
涓撳睘鐨勫唴瀛樺垎閰嶅嚱鏁?瀹冪殑杩斿洖鍊兼槸绫诲瀷鏈韩,鍥犱负杩欎笁绉嶇被鍨嬫湰韬甫鏈夋寚閽?鍙﹀make鍑芥暟瀵硅繖涓夌绫诲瀷鏈夋櫤鑳界殑鍒ゆ柇,瀵箂lice鏉ヨ,make鍑芥暟鏄繖绉嶆牱瀛?code>make(sliceType, length int, capacity int)sliceType
,瀹冩帴鍙椾笁涓弬鏁?鍒嗗埆鏄垏鐗囩被鍨?闀垮害,鍜屽閲?鍏朵腑瀹归噺鍙互鐪佺暐(鍜宭ength鐩稿悓),濡傛灉length澶т簬0, 鍒欏凡缁忚鍒嗛厤鐨勭┖闂翠細鍏堝瓨鏀剧被鍨嬮浂鍊?
澹版槑鏃跺垎閰嶅唴瀛樼殑鍐欐硶涓€
var s []int
s = make([]int,0)
fmt.Println(s) // []
var s []int
s = make([]int,3)
fmt.Println(s) // [0 0 0]
澹版槑鏃跺垎閰嶅唴瀛樼殑鍐欐硶浜?/p>
s := make([]int, 2)
涓嶄娇鐢╩ake鍑芥暟鍒嗛厤鍐呭瓨鐨勫啓娉?/p>
s := []int{}
杩欑鍐欐硶鍙兘鐢ㄤ簬:=
,鍏跺疄灏辨槸閫氳繃{}
鏉ヤ簨鍏堝垵濮嬪寲,宸茬粡鍒濆鍖栦簡,鑲畾涔熷氨鍒嗛厤鍐呭瓨浜?鍙笉杩囦笂闈㈢殑鍐欐硶鏄垵濮嬪寲涓虹┖鐨勫垵濮嬪寲,鍐嶄妇涓緥瀛愭洿濂界殑甯ぇ瀹剁悊瑙d竴涓?/p>
s := []int{1,2,3}
3. map
濡俿lice涓€鏍?map涔熸槸涓€涓甫鎸囬拡鐨勭被鍨?寮曠敤绫诲瀷)
-
涓€涓敊璇殑渚嬪瓙
var m map[string]interface{}
m["age"] = 1
panic: assignment to entry in nil map
閿欒鍘熷洜:map鏄紩鐢ㄧ被鍨?鏈垎閰嶅唴瀛樹究灏濊瘯鎿嶄綔鍏朵腑鐨勫唴瀹?/p>
-
make鍑芥暟瀵逛簬map
鍜宻lice涓嶅悓,make鍑芥暟瀵逛簬map鏄繖鏍峰瓙鐨?code>make(mapType, capacity int)mapType
,瀹冨彧鎺ュ彈涓や釜鍙傛暟鍒嗗埆鏄痬ap绫诲瀷鍜屽閲?濡傛灉浼犻€掑涓弬鏁颁細缂栬瘧鏃舵姤閿?鍜宻lice涓€鏍穋apacity鍙互琚渷鐣?鍊煎緱娉ㄦ剰鐨勬槸铏界劧绗簩涓€兼槸瀹归噺,浣嗘槸map骞朵笉鑳戒娇鐢?code>cap
鍑芥暟鑾峰彇鍏跺閲?鑰屼笖閫氳繃len
鍑芥暟鑾峰緱鏄痬ap鐨勯暱搴﹁€屼笉鏄閲?/strong>,涓句釜渚嬪瓙璇存槑涓€涓?/p>
m := make(map[string]interface{}, 10)
fmt.Println(len(m)) // 0
m["name"] = "horika"
// fmt.Println(cap(m)) // panic
fmt.Println(len(m)) // 1
閫氳繃make鍑芥暟缁檓ap鍒嗛厤鍐呭瓨鍐欐硶涓€
var m map[string]string
m = make(map[string]string, 1) // 瀹归噺鍙互琚渷鐣?m = make(map[string]string)
閫氳繃make鍑芥暟缁檓ap鍒嗛厤鍐呭瓨鍐欐硶浜?/p>
m := make(map[string]int)
涓嶉€氳繃make鍑芥暟鍒嗛厤鍐呭瓨鐨勫啓娉?/p>
m := map[int][int]{}
鍘熺悊鍜宻lice鐨勮繖绉嶅啓娉曚竴鏍?閫氳繃:=
鍜?code>{}
鏉ヨ繘琛屽垵濮嬪寲,涓€鏃﹁鍒濆鍖栦簡,涔熷氨琚垎閰嶅唴瀛樹簡.涓句釜鍒濆鍖栦笉涓虹┖鐨勪緥瀛?
m := map[int]int{
1: 1,
}
channel
鍦╣o璇█涓璫hannel鏄负浜?code>goroutine
鐨勯€氫俊,浠栦篃鏄竴绉嶅紩鐢ㄧ被鍨?channel
鐨勪娇鐢ㄥ繀椤婚€氳繃make
鍑芥暟鍒嗛厤鍐呭瓨.
-
涓€涓敊璇殑渚嬪瓙涓€
var c chan int // 瀹氫箟涓€涓紶閫抜nt鐨勯€氶亾 go printI(c) // 灏濊瘯鍚戜竴涓负鍒嗛厤鍐呭瓨鐨勯€氶亾鍙栧€? 杩欐槸浜嬪厛瀹氫箟鐨勬帴鏀朵竴涓猧nt閫氶亾鐨勫嚱鏁? // 濡傛灉娌℃湁杩欎竴鍙ヤ笅闈㈢殑鎿嶄綔浼氬牭濉?鍥犱负涓哄垎閰嶇殑閫氶亾鑲畾鏄病鏈夌紦鍐茬殑 c <- 1 // 灏濊瘯鍚戜竴涓病鏈夊垎閰嶅唴瀛樼殑閫氶亾浼犲€?
鏃犺鏄紶鍊艰繕鏄彇鍊奸兘浼氭姤閿?/p>
goroutine 1 [chan send (nil chan)]:
goroutine 18 [chan receive (nil chan)]:
-
涓€涓敊璇殑渚嬪瓙浜?/p>
var c chan int // 瀹氫箟涓€涓紶閫抜nt鐨勯€氶亾 close(c) // 灏濊瘯鍏抽棴涓€涓病鏈夊垎閰嶅唴瀛樼殑閫氶亾
panic: close of nil channel
濡傛灉鍏抽棴涓€涓病鏈夋病鏈夊垎閰嶅唴瀛樼殑閫氶亾涔熶細鎶ラ敊
-
make鍑芥暟瀵逛簬channel
涓庝互涓婁袱绉嶇被鍨嬮兘涓嶅悓make鍑芥暟瀵逛簬channel鏄繖鏍风殑
make(chanType, buffer int)chanType
,瀹冩帴鏀朵袱涓弬鏁?绗竴涓槸閫氶亾鐨勭被鍨?绗簩涓槸缂撳啿澶у皬(鐪佺暐鍒欐槸鏃犵紦鍐查€氶亾). -
鏀规涓婅堪渚嬪瓙
c := make(chan int) go printI(c) c <- 1 close(c)
鎴栬€?/p>
var c chan int c = make(chan int) go printI(c) c <- 1 close(c)
褰撶劧濡傛灉甯︽湁缂撳啿,浼犲€兼椂灏变笉鎬曢樆濉炰簡
c := make(chan int, 1) c <- 1 close(c)
以上是关于璋堣皥golang涓殑寮曠敤绫诲瀷涓庡湴鍧€鍒嗛厤的主要内容,如果未能解决你的问题,请参考以下文章