Golang Basic--method method

Source: Internet
Author: User
  • Go does not have a class-like concept in the Python language, but there are still method

    • The type method definition format is as follows
      func (a mytype) method_name(x type)(y type){ return y }
    • Call Mode:
      instance.method(args) -> (type).func(instance, args)
  • Implemented with a type (Struct,int ...) by displaying a description of receiver (the first argument that forces a function to pass). The combination

    package mainimport "fmt"type A struct{    Name Sting}func main() {    a := A{}                                        //实例化结构体    a.Method_Print()                                //结构体调用方法}func (a A) Method_Print() {                         //强制将A(struct类型)作为第一个参数传递给Method_Print,从而说明此函数属于结构A的一个方法    fmt.Println("This is a method for struct A")}/*输出This is a method for struct A*/
  • Methods can only be defined in the same package package 类型 and cannot be defined in non-packages 类型 , but the method (which refers to the public method) is callable in other packages.

  • Can invoke a method with only a value or a pointer, and the compiler will automatically complete the conversion

      package Mainimport "FMT" type A struct {Name string}type B str UCT {Name String}func main () {A: = a{}//Instantiate struct body a.method_print ()//struct call method FMT. PRINTLN (a.name)//pointer is passed without using ' *a.name ', the interpreter automatically recognizes B: = b{} fmt.    Println (B.name) b.method_print ()}func (a *a) Method_print () {//method pass struct pointer a.name = "Maozi" Fmt.                                 Println ("This was a method for struct a")}func (b b) Method_print () {//method: transitive structure b.name = "Sazi" This method can only make a copy of the value, unable to change the Structure property (field) Fmt. Println ("This was a method for struct B")}/* output A. Method_print---This is a method for struct aa.name---maozib. name--//Null value, modification failed B. Method_print this was a method for struct b*/ 
  • The
  • Type alias, which is an alias of type int, does not have the methods that are included with the underlying type, and can be any of the underlying types (int ... ) binds a method as follows Method_print

      package Mainimport "FMT" type int int To customize a type int, the underlying is an int type, and int is an alias for the underlying type int, func main () {var a INT a.method_print ()}func (a *int) method_ Print () {//Bind a method to the int type FMT. Println ("This was a method for int type")}/* output A. Method_print---This was a method for int type*/ 
  • Method Values and Method Expressions
    • Method Value is a wrapped state object that is always associated with a specific object instance
    • Called in the same way that a Python class is called, first instantiating a class, invoking it through a . property

    • Method expression is 类型.method(var) the direct way

    package mainimport "fmt"type INT int //定义一个INTfunc main() {    var a INT    a.Method_Print()                            //method value调用方式    (*INT).Method_Print(a)                      //method expression调用方式}func (a *INT) Method_Print() {                  //为int类型绑定一个方法    fmt.Println("This is a method for int type ")}/*输出a.Method_Print---This is a method for int type*/
  • A class method name conflict is similar to a field name conflict, in the order of inheritance, if the external structure and the embedded struct have a method with the same name, the method that invokes the external struct first

  • class method access rights: uppercase initials indicate externally accessible, lowercase denotes private fields of struct type, private fields and methods can only be called in the same package, only public methods (capitalized) can be called outside the package

    package mainimport "fmt"type A struct {    name string //小写首字母,私有字段} func main() {    a := A{}    a.Method_Print() //method value调用方式}func (a *A) Method_Print() {                                        a.name = "123"                                              //方法可以访问私有字段    fmt.Println("This is a method for struct type ")}/*输出a.Method_Print---This is a method for int type*/
  • Declares a type with the underlying int, and the implementation calls a method incrementing by 250, such as x:=0, called A. After increase (NUM), a changes from 0 to 250

        package mainimport "fmt"type Ad int                       //声明底层类型为int类型    func main() {    var x Ad    x.Add(250)                   //method value调用方式,方法传递参数num=100    fmt.Println(a)}func (ad *Ad) Add(num int) {    //为int类型绑定一个方法    *ad += Ad(num)              //强制类型转换为底层Ad---int型}/*输出a--->250*/
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.