Go的函數參數總是傳值

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

依照Go的FQA, 函數的參數傳遞總是傳值的(pass-by-value):

As in all languages in the C family, everything in Go is passed by value. That is, a function always gets a copy of the thing being passed, as if there were an assignment statement assigning the value to the parameter. For instance, passing an int value to a function makes a copy of the int, and passing a pointer value makes a copy of the pointer, but not the data it points to. (See a later section for a discussion of how this affects method receivers.)

Map and slice values behave like pointers: they are descriptors that contain pointers to the underlying map or slice data. Copying a map or slice value doesn't copy the data it points to. Copying an interface value makes a copy of the thing stored in the interface value. If the interface value holds a struct, copying the interface value makes a copy of the struct. If the interface value holds a pointer, copying the interface value makes a copy of the pointer, but again not the data it points to.

以下是測試代碼,結果貼在下面,不解釋了。

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
package mainimport ("fmt""reflect""unsafe")func printInt(i int) {fmt.Printf("int i: %p\n", &i)}func printInt2(i *int) {fmt.Printf("int i: %p\n", i)}func printStr(s string) {fmt.Printf("string s: %p\n", &s)hdr := (*reflect.StringHeader)(unsafe.Pointer(&s))data := hdr.Datafmt.Printf("string s data: 0x%x\n", data)}func printStr2(s *string) {fmt.Printf("string s: %p\n", s)hdr := (*reflect.StringHeader)(unsafe.Pointer(s))data := hdr.Datafmt.Printf("string s data: 0x%x\n", data)}func printSlice(s []int) {fmt.Printf("slice s: %p\n", &s)hdr := (*reflect.SliceHeader)(unsafe.Pointer(&s))data := hdr.Datafmt.Printf("slice s data: 0x%x\n", data)}func printSlice2(s *[]int) {fmt.Printf("slice s: %p\n", s)hdr := (*reflect.SliceHeader)(unsafe.Pointer(s))data := hdr.Datafmt.Printf("slice s data: 0x%x\n", data)}type S struct {I int}func printStruct(s S) {fmt.Printf("struct s: %p, I: %p\n", &s, &(s.I))}func printStruct2(s *S) {fmt.Printf("struct s: %p, I: %p\n", &s, &(s.I))}func printInterface(i interface{}) {fmt.Printf("int i: %p\n", &i)}func printInterface2(i interface{}) {s := i.(S)fmt.Printf("struct s: %p, I: %p\n", &s, &(s.I))}func printInterface3(i interface{}) {s := i.(*S)fmt.Printf("struct s: %p, I: %p\n", s, &(s.I))}func main() {//test inti := 10fmt.Printf("int i: %p\n", &i)printInt(i)printInt2(&i)//test strings := "hello, world"fmt.Printf("\n\nstring s: %p\n", &s)hdr := (*reflect.StringHeader)(unsafe.Pointer(&s))data := hdr.Datafmt.Printf("string s data: 0x%x\n", data)printStr(s)printStr2(&s)//slice and mapsl := []int{1, 2, 3, 4, 5}fmt.Printf("\n\nslice s: %p\n", &sl)hdr2 := (*reflect.SliceHeader)(unsafe.Pointer(&sl))data = hdr2.Datafmt.Printf("slice s data: 0x%x\n", data)printSlice(sl)printSlice2(&sl)//structss := S{I: 10}ssp := &ssfmt.Printf("\n\nstruct s: %p, I: %p\n", ssp, &(ss.I))printStruct(ss)printStruct2(ssp)//interface to intfmt.Printf("\n\nint i: %p\n", &i)printInterface(i)//interface to structfmt.Printf("\n\nstruct s: %p, I: %p\n", ssp, &(ss.I))printInterface2(ss)printInterface3(ssp)}

輸出結果:

123456789101112131415161718192021222324252627282930313233
int i: 0xc420074188int i: 0xc4200741b8int i: 0xc420074188string s: 0xc4200741c0string s data: 0xa778dstring s: 0xc4200741e0string s data: 0xa778dstring s: 0xc4200741c0string s data: 0xa778dslice s: 0xc42006e0c0slice s data: 0xc4200780c0slice s: 0xc42006e0e0slice s data: 0xc4200780c0slice s: 0xc42006e0c0slice s data: 0xc4200780c0struct s: 0xc420074210, I: 0xc420074210struct s: 0xc420074218, I: 0xc420074218struct s: 0xc420084020, I: 0xc420074210int i: 0xc420074188int i: 0xc420074230struct s: 0xc420074210, I: 0xc420074210struct s: 0xc420074228, I: 0xc420074228struct s: 0xc420074210, I: 0xc420074210

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.