Go語(yǔ)言中函數(shù)的參數(shù)傳遞與調(diào)用的基本方法
按值傳遞函數(shù)參數(shù),是拷貝參數(shù)的實(shí)際值到函數(shù)的形式參數(shù)的方法調(diào)用。在這種情況下,參數(shù)在函數(shù)內(nèi)變化對(duì)參數(shù)不會(huì)有影響。
默認(rèn)情況下,Go編程語(yǔ)言使用調(diào)用通過值的方法來傳遞參數(shù)。在一般情況下,這意味著,在函數(shù)內(nèi)碼不能改變用來調(diào)用所述函數(shù)的參數(shù)??紤]函數(shù)swap()的定義如下。
/* function definition to swap the values */
func swap(int x, int y) int {
var temp int
temp = x /* save the value of x */
x = y /* put y into x */
y = temp /* put temp into y */
return temp;
}
現(xiàn)在,讓我們通過使實(shí)際值作為在以下示例調(diào)用函數(shù)swap():
package main
import "fmt"
func main() {
/* local variable definition */
var a int = 100
var b int = 200
fmt.Printf("Before swap, value of a : %d\n", a )
fmt.Printf("Before swap, value of b : %d\n", b )
/* calling a function to swap the values */
swap(a, b)
fmt.Printf("After swap, value of a : %d\n", a )
fmt.Printf("After swap, value of b : %d\n", b )
}
func swap(x, y int) int {
var temp int
temp = x /* save the value of x */
x = y /* put y into x */
y = temp /* put temp into y */
return temp;
}
讓我們把上面的代碼放在一個(gè)C文件,編譯并執(zhí)行它,它會(huì)產(chǎn)生以下結(jié)果:
Before swap, value of a :100 Before swap, value of b :200 After swap, value of a :100 After swap, value of b :200
這表明,參數(shù)值沒有被改變,雖然它們已經(jīng)在函數(shù)內(nèi)部改變。
通過傳遞函數(shù)參數(shù),即是拷貝參數(shù)的地址到形式參數(shù)的參考方法調(diào)用。在函數(shù)內(nèi)部,地址是訪問調(diào)用中使用的實(shí)際參數(shù)。這意味著,對(duì)參數(shù)的更改會(huì)影響傳遞的參數(shù)。
要通過引用傳遞的值,參數(shù)的指針被傳遞給函數(shù)就像任何其他的值。所以,相應(yīng)的,需要聲明函數(shù)的參數(shù)為指針類型如下面的函數(shù)swap(),它的交換兩個(gè)整型變量的值指向它的參數(shù)。
/* function definition to swap the values */
func swap(x *int, y *int) {
var temp int
temp = *x /* save the value at address x */
*x = *y /* put y into x */
*y = temp /* put temp into y */
}
現(xiàn)在,讓我們調(diào)用函數(shù)swap()通過引用作為在下面的示例中傳遞數(shù)值:
package main
import "fmt"
func main() {
/* local variable definition */
var a int = 100
var b int= 200
fmt.Printf("Before swap, value of a : %d\n", a )
fmt.Printf("Before swap, value of b : %d\n", b )
/* calling a function to swap the values.
* &a indicates pointer to a ie. address of variable a and
* &b indicates pointer to b ie. address of variable b.
*/
swap(&a, &b)
fmt.Printf("After swap, value of a : %d\n", a )
fmt.Printf("After swap, value of b : %d\n", b )
}
func swap(x *int, y *int) {
var temp int
temp = *x /* save the value at address x */
*x = *y /* put y into x */
*y = temp /* put temp into y */
}
讓我們把上面的代碼放在一個(gè)C文件,編譯并執(zhí)行它,它會(huì)產(chǎn)生以下結(jié)果:
Before swap, value of a :100 Before swap, value of b :200 After swap, value of a :200 After swap, value of b :100
這表明變化的功能以及不同于通過值調(diào)用的外部體現(xiàn)的改變不能反映函數(shù)之外。
相關(guān)文章
Golang實(shí)現(xiàn)Biginteger大數(shù)計(jì)算實(shí)例詳解
這篇文章主要為大家介紹了Golang實(shí)現(xiàn)Biginteger大數(shù)計(jì)算實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
深入學(xué)習(xí)Golang并發(fā)編程必備利器之sync.Cond類型
Go?語(yǔ)言的?sync?包提供了一系列同步原語(yǔ),其中?sync.Cond?就是其中之一。本文將深入探討?sync.Cond?的實(shí)現(xiàn)原理和使用方法,幫助大家更好地理解和應(yīng)用?sync.Cond,需要的可以參考一下2023-05-05
go?zero微服務(wù)實(shí)戰(zhàn)系服務(wù)拆分
這篇文章主要為大家介紹了go?zero微服務(wù)實(shí)戰(zhàn)系服務(wù)拆分的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
Golang中Channel實(shí)戰(zhàn)技巧與一些說明
channel是Go語(yǔ)言內(nèi)建的first-class類型,也是Go語(yǔ)言與眾不同的特性之一,下面這篇文章主要給大家介紹了關(guān)于Golang中Channel實(shí)戰(zhàn)技巧與一些說明的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-11-11
Golang關(guān)鍵字select的常用用法總結(jié)
這篇文章主要為大家詳細(xì)介紹了golang中select關(guān)鍵字的常用用法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-10-10
golang實(shí)現(xiàn)簡(jiǎn)單rpc調(diào)用過程解析
這篇文章主要介紹了golang實(shí)現(xiàn)簡(jiǎn)單rpc調(diào)用,包括RPC具體實(shí)現(xiàn)結(jié)合實(shí)例代碼給大家講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05
Goland 斷點(diǎn)調(diào)試Debug的操作
這篇文章主要介紹了Goland 斷點(diǎn)調(diào)試Debug的操作方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-04-04
Go語(yǔ)言使用slices包輕松實(shí)現(xiàn)排序功能
在 Go 語(yǔ)言開發(fā)中,對(duì)數(shù)據(jù)進(jìn)行排序是常見的需求,Go 1.18 版本引入的 slices包提供了簡(jiǎn)潔高效的排序解決方案,支持內(nèi)置類型和用戶自定義類型的排序操作,本文將通過具體示例,詳細(xì)介紹如何使用 slices包實(shí)現(xiàn)排序及相關(guān)功能,需要的朋友可以參考下2025-05-05

