This is a creation in Article, where the information may have evolved or changed.
This article ignores anonymous struct and struct anonymous methods, generally as developers do not recommend to play this kind of technical but not meaningful things, directly from the structure of the value of the transfer and the reference to pass the start
2. Value passing and reference passing
No matter what language you learn, it's almost a matter of talking about "value passing" and "reference passing", and the Go language is also a tacky one, in the go language, in addition to slicing (slice), set (map), channels (channel) and Interface (interface), the other is the value of the transfer, Look at the following example:
/* Declare a struct */ Type employee struct { Name, address string//name, address Height, weight float64//height, weight } /* Define the method, which is a struct, which modifies the value of struct member name */ Func ModifyAttribute (EMP employee) { Emp.name = "Newer" Fmt. PRINTLN (EMP) } /* Test method */ Func Main () { Initialize struct and assign to EMP EMP: = employee{name: "Eagle", Address: "Guangdong", Height:172.0, weight:75.3} Print results before modifying Fmt. PRINTLN (EMP) Call method modifies the name value and prints ModifyAttribute (EMP) Print results after modification Fmt. PRINTLN (EMP) } |
Execute and print the results
As you can see from the results, although name is modified in method ModifyAttribute, it is not affected by the EMP defined in the main method, so this is a value pass.
C language After so long the ups and downs and enduring, one of the reasons is that its operation on memory, memory operation means that the performance is improved, because the structure of the storage address of the operation efficiency is much higher than the structure of the replication (don't tense, here do not speak hands, hehe)
Next we'll change the entry from struct to struct pointer
/* Change the entry from struct to struct pointer */ Func modifyattribute (emp *employee) { Emp.name = "Newer" Fmt. PRINTLN (EMP) } /* Test method */ Func Main () { Initialize struct and assign to EMP EMP: = employee{name: "Eagle", Address: "Guangdong", Height:172.0, weight:75.3} Print results before modifying Fmt. PRINTLN (EMP) Call method modifies the name value and prints ModifyAttribute (&emp) Print results after modification Fmt. PRINTLN (EMP) } |
Execute and print the results
It can be seen from the results that the modification of the method affects the value of the EMP in the Main method, which is no longer a value passing, but a reference passed:)
3, again talk about nested structure body
Because the structure of the nested in the specific coding often appear, the last section is too hasty, afraid not explained clearly, here again on the structure of the nesting problem
/* Declare figure structure Body */ Type Figure struct { Height, weight float64 } /* Declare human structure, inside nested figure struct */ Type human struct { Name, address string Figure } |
As Oberzhang says, a struct is initialized before it is used, and a better understanding is initialized by: literal literals.
Man: = human{}//Initialize human struct, but do not assign value to member
Use literal values for assignment
Man.name = "Eagle"
man.address = "Guangdong"
Man.height = 172//or man.figure.height = 172
Man.weight = 75.3//or man.figure.weight = 75.3
This assignment is more object-oriented, and readers who are transitioning from Java or C + + may prefer it, but in the real coding process, we often read code written by others, who may be more accustomed to initializing and assigning a piece:
Man: = human{name: "Eagle", Address: "Guangdong", Figure:figure{height:172, weight:75.3}}
Please take a break and then focus on the following:
In human structure nesting, you will find that the member variable name has the specified name and type, and the same address is, but there is a wonderful figure, is it a member name? Or the structure type?
The answer is that figure is both a member name and a type, so it is initialized in the form of figure:figure{} at initialization time.
If you understand this sentence, you can then look down, or need to reread several times, if not clear words can leave a message to me:)
4. Object-oriented
In the "4" Go language type and the method for type addition mentioned as the type of Add method, you have to digest:
An alias integer for int type Type Integer int Add a new method to type integer LessThan Func (a integer) LessThan (b integer) bool{ Return a < b } And then you can object-oriented programming. var a integer = 5//Defines an object a whose type is integer A.lessthan (8)//method of calling Object a LessThan () |
And look at the statement of the struct.
| An alias for a struct type employee Type Employee struct{ Name, address string } Simulating a constructor with a method Func NewEmployee (name, address string) employee{ return employee{name, address} } Define a method for modifying an employee's address Func modifyaddress (emp *employee) { emp.address = "Shenzhen" } Add a comparison method for type employee: If the name and address are the same, the two objects are considered the same Func (SRC employee) isequal (dest employee) bool{ return src.name = = Dest.name && src.address = = dest.address } Using the structure in an object-oriented programming way Initialize the object src and dest, and both give the same value var src = newemployee ("Eagle", "Guangdong") var dest = NewEmployee ("Eagle", "Guangdong") Fmt. PRINTLN (src. IsEqual (dest))//print result is true Modify the address of the target Modifyaddress (&dest) Fmt. PRINTLN (src. IsEqual (dest))//print result is false |
Well, to this structure basically has been introduced, but there are some more details of things not introduced, such as:
Initializes the struct with a new method
A struct is a composite type, so it can be combined with pointers, and this article does not involve in depth
Wait a minute