璋堣皥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涓殑寮曠敤绫诲瀷涓庡湴鍧€鍒嗛厤的主要内容,如果未能解决你的问题,请参考以下文章

璋堣皥Nginx绾跨▼姹狅紝瀹炵幇1涓嚎鐨勫皬鐩爣锛?/h1>

JS涓殑鏁版嵁绫诲瀷

銆岀25璁层€?璋堣皥 JVM 鍐呭瓨鍖哄煙鍒掑垎锛屽摢浜涘尯鍩熷彲鑳藉彂鐢?OutOfMemoryError?|瀛︿範绗旇

浠庝竴閬撻潰璇曢璋堣皥瀵?EventLoop 鐨勭悊瑙?/h1>

python瀛︿範鍩虹涓€

DOM-Text绫诲瀷銆丆omment绫诲瀷銆丆DATASection绫诲瀷銆丏ocumentType绫诲瀷銆丏ocumentFragment绫诲瀷銆丄ttr绫诲瀷