?
Dieses Dokument verwendet PHP-Handbuch für chinesische Websites Freigeben
傳遞是指在調用函數(shù)時將實際參數(shù)復制一份傳遞到函數(shù)中,這樣在函數(shù)中如果對參數(shù)進行修改,將不會影響到實際參數(shù)。
默認情況下,Go 語言使用的是值傳遞,即在調用過程中不會影響到實際參數(shù)。
以下定義了 swap() 函數(shù):
func swap(x, y int) int { var temp int temp = x x = y y = temp return temp; }
接下來,讓我們使用值傳遞來調用 swap() 函數(shù):
package main import "fmt" func main() { var a int = 100 var b int = 200 fmt.Printf("交換前 a 的值為 : %d\n", a ) fmt.Printf("交換前 b 的值為 : %d\n", b ) swap(a, b) fmt.Printf("交換后 a 的值 : %d\n", a ) fmt.Printf("交換后 b 的值 : %d\n", b ) } func swap(x, y int) int { var temp int temp = x x = y y = temp return temp; }
以下代碼執(zhí)行結果為:
交換前 a 的值為 : 100 交換前 b 的值為 : 200 交換后 a 的值 : 100 交換后 b 的值 : 200
關于我們 聯(lián)系我們 留言板
手冊網(wǎng)