Difference between function and method in Golang

Before I came into contact with go, I thought that function and method were just two names for the same thing (there is no obvious difference in c/c++, python, java that I am familiar with), but in golang they are completely different thing. The official explanation is that the method is a function that contains the receiver. What does it mean.

First of all, the format of the function is fixed, func + function name + parameter + return value (optional) + function body. example 

func main() 

{

fmt.Println("Hello go")

}

There are two special functions in golang, the main function and the init function. The main function does not need to be introduced and is the same in all languages. As the entry point of a program, there can only be one. The init function is optional in each package, optional, or even multiple (but one init function in a package is strongly recommended). The init function will automatically call the init function when you import the package, so init The function does not need to be called manually, and it will only be called once, because when a package is referenced multiple times, it will only be imported once.

package mypackage  
  
import (  
    "fmt"  
)  
  
var I int  
  
func init() {  
    I = 0  
    fmt.Println("Call mypackage init1")  
}  
  
func init() {  
    I = 1  
    fmt.Println("Call mypackage init2")  
}  

 

package main  
  
import (  
    "demo/mypackage"  
    "fmt"  
)  
  
func main() {  
    fmt.Println("Hello go.... I = ", mypackage.I)  
}  

operation result:

We can see that the program automatically calls two init functions for us, and they are called in sequence.

See the method below.

 

package main  
  
import "fmt"  
  
type myint int  
  
//乘2  
func (p *myint) mydouble() int {  
    *p = *p * 2  
    return 0  
}  
  
//平方  
func (p myint) mysquare() int {  
    p = p * p  
    fmt.Println("mysquare p = ", p)  
    return 0  
}  
  
func main() {  
    var i myint = 2  
    i.mydouble()  
    fmt.Println("i = ", i)  
    i.mysquare()  
    fmt.Println("i = ", i)  
}  

operation result:

We can see the difference between a method and a function. A method is a receiver instead of a function name after the func keyword. The receiver can be a type defined by itself. This type can be struct, interface, or even we can redefine basic data types. . We can give him some methods we want to meet the needs in our actual project, just like the above I redefine int and give it a method of multiplying by 2 and balancing, here we have to pay attention to a detail, the receiver It is the difference between a pointer and a non-pointer. We can see that when the receiver is a pointer, we can change the properties of the receiver through methods, but the non-pointer type cannot do it.

The receiver here is somewhat similar to the this pointer in C++. We can regard the receiver as a class, and these methods are the member functions of the class. When the receiver is a pointer type, it is a non-const member function in C++. When it is not a pointer, it is a const member function, and the tired member variables cannot be changed by this method.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325470902&siteId=291194637