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

go語(yǔ)言中參數(shù)傳遞 golang 參數(shù)傳遞

Go 函數(shù)參數(shù)傳遞詳解

前言:go語(yǔ)言函數(shù)參數(shù)為值拷貝(指針參數(shù)為指針拷貝)。

成都創(chuàng)新互聯(lián)成立于2013年,先為巫溪等服務(wù)建站,巫溪等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為巫溪企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問(wèn)題。

在go語(yǔ)言中,函數(shù)也作為一種數(shù)據(jù)類型,所以函數(shù)也可以作為函數(shù)的參數(shù)來(lái)使用。

其中slice是為地址數(shù)組指針的拷貝??,持續(xù)更新中 ....

golang的不定參數(shù)怎么傳遞下去

給你個(gè)fmt.Printf的例子:

echo 函數(shù)不定參數(shù),其調(diào)用fmt.Printf進(jìn)行輸出,因?yàn)関是一個(gè)slice,所以傳遞給fmt.Printf的時(shí)候需要 v...,就類似append(slice1,slice2...)

package main

import (

"fmt"

)

func main() {

echo("Hello %s, I am %s\n", "Bob", "John")

}

func echo(format string, v ... interface{}) {

fmt.Printf(format, v...)

}

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

注:本文是對(duì) golang-101-hacks 中文翻譯。

在Go語(yǔ)言中,函數(shù)參數(shù)是值傳遞。使用slice作為函數(shù)參數(shù)時(shí),函數(shù)獲取到的是slice的副本:一個(gè)指針,指向底層數(shù)組的起始地址,同時(shí)帶有slice的長(zhǎng)度和容量。既然各位熟知數(shù)據(jù)存儲(chǔ)的內(nèi)存的地址,現(xiàn)在可以對(duì)切片數(shù)據(jù)進(jì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:

運(yùn)行結(jié)果如下

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

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函數(shù)并沒(méi)有修改main函數(shù)中的切片s的元素。這是因?yàn)樗皇遣僮髑衅瑂的副本,而不是切片s本身。所以如果真的想讓函數(shù)改變切片的內(nèi)容,可以傳遞切片的地址:

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:

運(yùn)行結(jié)果如下

【Go參數(shù)傳遞:值類型、引用類型和指針類型】

修改參數(shù)

值類型

指針類型

引用類型

chan

類型零值

總結(jié) :在Go語(yǔ)言中, 函數(shù)的參數(shù)傳遞只有值傳遞 ,而且傳遞的實(shí)參都是原始數(shù)據(jù)的一份拷貝。如果拷貝的內(nèi)容是值類型的,那么在函數(shù)中無(wú)法修改原始數(shù)據(jù),如果拷貝的內(nèi)容是指針(或者可以理解為引用類型),那么可以在函數(shù)中修改原始數(shù)據(jù)。

當(dāng)前名稱:go語(yǔ)言中參數(shù)傳遞 golang 參數(shù)傳遞
本文路徑:http://chinadenli.net/article40/hhgpeo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站品牌網(wǎng)站制作網(wǎng)站導(dǎo)航建站公司企業(yè)建站域名注冊(cè)

廣告

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

外貿(mào)網(wǎng)站制作