function parameters of go always pass value

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

According to the fqa of Go, the parameters of the function are always passed the value (pass-by-value):

As in all languages with the C family, everything in Go are passed by value. That's, a function always gets a copy of the thing being passed, as if there were an assignment statement assigning the V Alue 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 isn't the data it points to. (see a later sections for a discussion of what this affects method receivers.)

Map and slice values behave like pointers:they is 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.

The following is the test code, the results are posted below, not explained.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 66676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
 PackageMainImport("FMT""Reflect""unsafe")funcPrintint (iint) {FMT. Printf ("int i:%p\n", &i)}funcPrintInt2 (i *int) {FMT. Printf ("int i:%p\n", i)}funcPRINTSTR (Sstring) {FMT. Printf ("string s:%p\n", &s) HDR: = (*reflect. Stringheader) (unsafe. Pointer (&s)) Data: = HDR. datafmt.printf ("string s data:0x%x\n", data)}funcPRINTSTR2 (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)}funcPrintslice (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)}funcPrintSlice2 (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)}typeSstruct{Iint}funcPrintstruct (s s) {FMT. Printf ("struct S:%p, I:%p\n", &s, & (S.I))}funcPrintStruct2 (s *s) {fmt. Printf ("struct S:%p, I:%p\n", &s, & (S.I))}funcPrintinterface (iInterface{}) {Fmt. Printf ("int i:%p\n", &i)}funcPrintInterface2 (iInterface{}) {s: = i. (s) fmt. Printf ("struct S:%p, I:%p\n", &s, & (S.I))}funcPrintInterface3 (iInterface{}) {s: = I. (*S) FMT. Printf ("struct S:%p, I:%p\n", S, & (S.I))}funcMain () {//test intI: =TenFmt. 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:Ten}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)}

Output Result:

123456789101112131415161718192021222324252627282930313233
intI0xc420074188intI0xc4200741b8intI0xc420074188string 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:0xc420074210intI0xc420074188intI0xc420074230struct S:0xc420074210, I:0xc420074210struct S:0xc420074228, I:0xc420074228struct S:0xc420074210, I:0xc420074210

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.