Value passing of a function in Golang

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

Today there is a confusing place where a lot of novices may encounter (especially in Java before)

The properties of the object-oriented part of the Golang (the bottom line is that process is a method of the mysession struct, and the Golang uses a snippet of code to add a code fragment to a struct or class that is subordinate to his execution method, The attributes you have are defined in-house, as in Java. )

Type MySession struct{

In string

Out string

}


Func (Session mysession) process (Xmlstr string) {
.........
}

And

Func (Session *mysession) process (Xmlstr string) {
.........
}

The difference

*mysession represents the mysession of the reference address, and mysession is a struct, if the thinking of Java is to think that the complex data type should be a reference pass, within the function of the mysession change is useful

And not in Golang, most of the Golang are value passing including structs, arrays, except slice map channel is a reference pass, for Func (Session mysession) process (Xmlstr string {} is a copy of the mysession, the function inside the functions will not reflect the original mysession, and the Func (session *mysession) process (Xmlstr string) {} can, because * MySession is a reference address, the reference address value passed the copy of the reference address, the role of it is to reflect the original structure, if it is a complex struct, and forget to add *, then the entire structure is copied value is passed, the performance can be imagined very bad, And you're not going to affect the outside structure at all.

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.