Go-method method

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

1. Values and references
VarName2 = varName1
Varname2.funcname ()
Value type (pass-through) if varName2 is changed and varName1 is not changed
If the varName2 changes and the varName1 changes, then the reference type (pass-through)

2. Method
Format:
  Func (R [*]receivertype) FuncName (param) (result) {...}
Note:
a. R is the recipient of a struct object, with different receivers and different methods
b. R can be passed for a value or can be passed as a reference
C. Param and result are optional, as with normal functions
D. Call the method with a "." Connection, i.e. R.funcname ()
e. Func (R *receivertype) FuncName () and func (R Receivertype) FuncName cannot exist simultaneously (here FuncName same)
F. Method can be used for all types
Cases:
Type Circle struct {  radius float64}func main () {  var c1 Circle  C1.radius = 10.00  fmt. Println ("Area of Circle (C1) =", C1.getarea ())}//the method belongs to the methods in the Circle type Object func (c Circle) Getarea () float64 {  //c.ra Dius is the property in the Circle type Object  return 3.14 * C.radius * C.radius}

In an object-oriented manner, the element declared in struct = = Class,struct is a member property, and Circle acts as the receiver of the function as a member method
That is, the circle class name, radius and Getarea () are member properties and member methods of the Circle class object C1, respectively
Example 1 (value pass):
Type Circle struct {  radius float64}func main () {  var c1 Circle  C1.radius = 10.00  fmt. Println ("Main (): Radius =", C1.radius)  FMT. Println ("Area of Circle (C1) =", C1.getarea ())}func (c Circle) Getarea () float64 {  fmt. Println ("Getarea (): Radius =", C.radius)  return 3.14 * C.radius * C.RADIUS}FUNC (c Circle) Setradius (R float64) {
  
   c.radius = R  fmt. Println ("Setradius (): Radius =", C.radius)}
  
Results:
Setradius (): Radius = 10
Main (): Radius = 0
Getarea (): Radius = 0
Area of Circle (c1) = 0
Example 2 (reference pass)
Type Circle struct {  radius float64}func main () {  var c2 Circle  C2.setradius (10.00)  FMT. Println ("Main (): Radius =", C2.radius)  FMT. Printf ("Radius of Circle (C2) =%.2f, area =%.2f", C2.radius, C2.getarea ())}func (c *circle) Setradius (R float64) {  C. Radius = r  fmt. Println ("Setradius (): Radius =", C.radius)}func (c *circle) Getarea () float64 {  fmt. Println ("Getarea (): Radius =", C.radius)  return 3.14 * C.radius * C.radius}
Results:
Setradius (): Radius = 10
Main (): Radius = 10
Getarea (): Radius = 10
Radius of Circle (C2) = 10.00, area = 314.00
Supplement:
Method can be used for all types
Define the format:
Type Custom Type
such as: type integer int, which is similar to defining an alias for int, but at this point the integer is the same as the type used by the struct definition, and it can be added as a class.
Example:
Type integer IntFunc main () {  var i Integer  fmt. Println (I.gettype ())  FMT. Println (Integer.gettype (i))  integer.print (i, "I am Int")}func (i Integer) getType () string {  return "Integer = = I NT "}func (I Integer) Print (s string) {  FMT. Println (s)}
Results:
Integer = = int
Integer = = Int
I am Int
Note:
a. When the recipient is typeName, not *typename, the calling method Varname.funcname (param) is the same as typename.funcname (VarName, param), and if it is *typename it cannot be accessed
b. If the person is *typename, access by Varname.funcname () equals (&varname). FuncName (), that is, you can not add "&" to the address in front of the VarName

3. Method inheritance and overloading
Anonymous fields are similar to inherited member properties in object-oriented programming or can be overloaded with member properties, and method can also inherit and overload
3.1 Inheritance Examples:
Similar to calling a method in a parent class that does not have a child class
No private protected public keyword in object-oriented (judged by the case of the first letter of the method name)
Type person struct {  name string age  int}type Employee struct {  person  salary Int}func main () {  //var Em1 Employee = employee{person{"Rain", "Up to", "  em1": = employee{person{"Rain", "%",  em1.printmsg ()}func (P person) printmsg () {  fmt. Println ("I am", p.name, ", and my Age is", P.age)}
Results:
I am Rain, and my age is
3.2 Overloading Example
Overloaded methods, which are accessed through Varname.funcname (), also take the principle of outermost first access, and are similar to methods in object-oriented invocation of this class
by VarName. Anonymous field. FuncName () accesses methods in the specified anonymous field, similar to those in object-oriented calls to the parent class
Type person struct {  name string age  int}type Employee struct {  person  salary Int}func main () {  //var Em1 Employee = employee{person{"Rain", "Up to", "  em1": = employee{person{"Rain", "%", "  em1.printmsg" ()       Call the outermost (this class) method  em1. Person.printmsg ()//invokes the method that specifies the anonymous field (parent Class)}func (P person) printmsg () {  fmt. Println ("I am", p.name, ", and my Age are", P.age)}func (e Employee) printmsg () {  fmt. Printf ("I am%s, my age was%d, and my salary is%d. \ n", E.name, E.age, E.salary)}
Results:
{{rain 23} 5000}
I am Rain, my age is at, and my salary is 5000.
I am Rain, and my age is

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Related Article

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.