Golang Main and Init, functions and methods

Source: Internet
Author: User

The

has two special functions in Golang, the main function and the Init function, and the main function does not have to be described in all languages, it acts as a portal to a program and can only have one. The init function is optional in each package, dispensable, and can even have multiple (but strongly recommend an init function in the packages), and the Init function automatically calls the INIT function when you import it, so the init function does not need to be called manually by us. L In addition, it will only be called once, because when a package is referenced more than once, it is only imported once.

 package mypackage import (  " fmt   " )  Span style= "COLOR: #0000ff" >var  I int   func Init () {I  = 0   FMT. Println (  " call mypackage init1  Span style= "COLOR: #800000" > "  = 1   FMT. Println (  " call mypackage init2  Span style= "COLOR: #800000" > " )}  
Package main Import (    "demo/mypackage"    "fmt"  ) Func main () {    fmt. Println ("", MyPackage. I)}

Operation Result:

Call MyPackage init1
Call MyPackage Init2


------------------------------------------------------------------------------------------Ornate Split Line---------------- ------------------------------------------------------------------------

function definition: func function_name (param1, param2) (return_type) Eg:
int string) (intstringint)   // The first parenthesis is your argument list, and the second parenthesis is your list of return values     }

See more: https://www.cnblogs.com/skymyyang/p/7659775.html

Definition of the method: Func (Receiver_name receiver_type) function_name () (return_type) Eg:
int int //A is a receive parameter of type int, the return value is int    }    

Classic examples of methods

Package Main Import"FMT"Type Myintint //by 2Func (P *myint) MyDouble ()int {    *p = *p *2    return 0} //SquareFunc (P Myint) MySQUARE ()int{p= P *P FMT. Println ("MySQUARE p =", p)return 0} func Main () {varI myint =2i.mydouble () fmt. Println ("i =", i) i.mysquare () fmt. Println ("i =", i)}

Operation Result:

i = 4
MySQUARE p = 16
i = 4

We can see the difference between the method and the function, the method after the Func keyword is the recipient instead of the function name, the receiver can be a type of its own definition, the type can be struct,interface, and even we can redefine the base data type. We can give him some of the methods we want to meet our actual engineering needs, just like the one above I redefined int and gave it a way to multiply the 2 peace law, here we pay attention to a detail, the receiver is a pointer and non-pointer difference, we can see when the receiver is a pointer, We can change the recipient's properties by method, but the non-pointer type is missing .
There are some similarities between the recipient and the this pointer in C + +, we can treat the receiver as a class, and these methods are the member functions of the classes, when the recipient is a pointer-type is a non-const member function in C + + and is a const member function for non-pointers. You cannot change a tired member variable by this method.

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.