欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

go語言黑魔法 黑魔法code

golang-101-hacks(12)——切片作為函數參數傳遞

注:本文是對 golang-101-hacks 中文翻譯。

成都創(chuàng)新互聯(lián)專注于蓮花企業(yè)網站建設,自適應網站建設,商城網站建設。蓮花網站建設公司,為蓮花等地區(qū)提供建站服務。全流程定制開發(fā),專業(yè)設計,全程項目跟蹤,成都創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務

在Go語言中,函數參數是值傳遞。使用slice作為函數參數時,函數獲取到的是slice的副本:一個指針,指向底層數組的起始地址,同時帶有slice的長度和容量。既然各位熟知數據存儲的內存的地址,現(xiàn)在可以對切片數據進行修改。讓我們看看下面的例子:

In Go, the function parameters are passed by value. With respect to use slice as a function argument, that means the function will get the copies of the slice: a pointer which points to the starting address of the underlying array, accompanied by the length and capacity of the slice. Oh boy! Since you know the address of the memory which is used to store the data, you can tweak the slice now. Let's see the following example:

運行結果如下

由此可見,執(zhí)行modifyValue函數,切片s的元素發(fā)生了變化。盡管modifyValue函數只是操作slice的副本,但是任然改變了切片的數據元素,看另一個例子:

You can see, after running modifyValue function, the content of slice s is changed. Although the modifyValue function just gets a copy of the memory address of slice's underlying array, it is enough!

See another example:

The result is like this:

而這一次,雀山型addValue函數并沒有修改main函數中的切片s的元素。這是因為它只是操作切片s的副本,而不是切片s本身。所以如果真的想讓函數唯枝改變切片的頃猜內容,可以傳遞切片的地址:

This time, the addValue function doesn't take effect on the s slice in main function. That's because it just manipulate the copy of the s, not the "real" s.

So if you really want the function to change the content of a slice, you can pass the address of the slice:

運行結果如下

名稱欄目:go語言黑魔法 黑魔法code
網站地址:http://chinadenli.net/article43/dsphehs.html

成都網站建設公司_創(chuàng)新互聯(lián),為您提供微信公眾號微信小程序自適應網站建站公司網站收錄

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

小程序開發(fā)