(Xiv) golang-- functions and packages

1. how defined functions?

func (parameter list) returns a list of values ​​{

  Perform an operation

  return 

}

2. What is the package?

The nature of the package is a folder, stored program files

Three main functions:

  • Distinguishing the same function name, and other variables identifier;
  • When a lot of program files, you can well manage the project;
  • Nothing range control functions, variables, i.e. scope;

Note: Go language provides two critical paths, GOROOT pointing system installation path, GOPATH point to the working path, this advantage is that our work can be separated and system files. You can go env view the environment. When importing the package, go look for the default language from the src file in the $ GOPATH, that is to say, we have to build the project file in the src folder inside GOPATH path, (although the project can go mod init placed anywhere location, but this time being not learned). To change GOPATH path, the main need to set the path they want in the system environment variables in the environment:

 

 For example, my GOPATH path described above, I created the following directory structure:

 

 Utils package that we want in project_2 with project_1, you can import a path out of the red box at the front (from the default src find below), the test results:

 

 Details of the package:

  • In general, the package name and go file names are the same;
  • When a reference file functions and variables to other packages, to be introduced into the corresponding bag;
  • package must be placed on the first line of the file;
  • Looking from the package will automatically find under src GOPATH path; (of course, may also be provided)
  • To be used by other file, function name or variable packet inside the first letter should be capitalized;
  • When reference to the function of other packages, the package name is the function name.;
  • After go to the package supports alias, alias taken, can not formerly used: e.g. util "go_code / project_1 / utils";
  • In the same package, you can not have the same function name, can not have the same global variables;
  • If you want compiled into an executable file, you need to declare on the package for the main

 Language function call mechanism 3.go

 

 4. recursive function calls

A function in the body and function calls itself, this is the recursive call.

 

As another example, note that the red box marked:

 

 Recursive functions to comply with the principles of:

  • When a function is executed, it is necessary to create a new independent space protected (new function stack)
  • Local variables are independent and do not affect each other;
  • Recursive must exit the recursive approach to the conditions, otherwise it will be an infinite recursion;
  • When a function is finished, or encountered return, will return, observe who called the results will return to whom;

 

 

Guess you like

Origin www.cnblogs.com/xiximayou/p/11874140.html