Functions, packages and error handling

#### Go function, including (a) 
*** Heaven, astrotech Zundao constant length ***
##### function
is a function in order to complete the set of program statements, the function points in Go user-defined function, the system function
function syntax:
FUNC function name (parameter list) (the returned list) {
function statements
return return value list
}
case:
package main

import "fmt"

func add(a,b int) int{
   return a + b
}
func main() {
   var sum int
   sum = add(10,20)
   fmt.Println(sum)
}

  


##### packages
in development, we often need to call other functions defined in the file; in fact, the essence of the package is to create a different folder to store the program files;
Go every program file belongs to a package, it can be said in the form of file management and project directory structure is go pack;
the role of ###### package:
1. distinguishing identifier of the same name functions, variables, etc.;
2 can be a good project management;
3. access control function functions, variables, etc., that is the scope;

package main

import (
   "ReadingNovels/src/utils"
   "fmt"
)

func main() {
   var sum int
   sum = utils.Add(10,20)
   fmt.Println(sum)
}

  


Notes package:
When given a file package, the package corresponds to a folder, for example utils file corresponding to the package name is utils, package name usually files of the same folder name the same, usually lower-case letters;
when a folder to use functions or variables other packages, the package needs to introduction
1. Entry mode 1: import "package name"
2. introduction of embodiment 2: Import (
"package name"
"package name"
)
3. package instruction must first line
4. If you want to access functions or other packages of the present package variables, function name or variable *** need initials ***
5. access syntax package name. package name or function name variable name
6. If the name of the long packet to the packet can alias: taken after completion alias name can not use the original package, the package must use the new name; product
7. in the same package can not have the same name and function name of the global variable
8. to compiled into an executable program, this package needs to be declared as the main, if it is a library package name can be customized;
package main
import (
   util "ReadingNovels/src/utils"
   "fmt"
)

  


---
###### function call mechanism
package main

import (
   "fmt"
)
func add(a int){
   a += 10
   fmt.Println(a) // 11
}

func main() {
   var sum = 1
   add(sum)
   fmt.Println(sum)//1 
}

  


Note:
1. When calling a function, the function will allocate a new space, the compiler will allow through its own process the new space from the other spaces;
2. space corresponding to each function, the data space are independent and not be confused;
3. when a function call is completed, the program will destroy the function corresponding to the space;
4. for the above example: add value becomes the internal function, the main function value is not changed
---
### ### return
Go function supports returning multiple values in
func function name (parameter list) (the returned list) {
function body
return to return a list of values
}
1. If the return multiple values, upon receiving ignore a desired value, use "_" symbol occupying ignored;
2. If the return value is only one value in the list may not need to return ();
Package main 

Import ( 
   "FMT" 
) 
FUNC Calc (A, B int) (int, int) { 
   return A + B, ab & 
} 

FUNC main () { 
   var A =. 1 
   var B =. 3 
   SUM, Sub: = Calc (A , B) 
   fmt.Println (SUM, Sub) //. 4, -2 
   // if you want to ignore a value may be used _ 
   SUM, Calc _ = (A, B) 
}

  


---
###### recursive function
if a function in the body and function calls itself, we call this function recursive function;
import "fmt"

func recurse(n int){
   if n > 2 {
      n--
      recurse(n)
      fmt.Println(n)
   }
}
func main() {
   recurse(5)
}

  


Recursive function key principles you need to follow:
1. recursion recursion conditions must be approaching the exit, or is infinite recursion;
2. When a function is finished or return encounter will return, who call will abide by the results returned to whom, and when when the function is finished or return, the system function itself will be destroyed;
package main

import "fmt"

func recurse(n int)int{
   if n ==1 || n==2 {
      return 1
   }else {
      return recurse(n-1) + recurse(n-2)
   }
}
func main() {
   fmt.Println(recurse(8))
}

  


---
function and details Precautions
1. The parameter list may be a plurality of functions, the return value may be a plurality of lists;
2. the parameter list and returns a list of data type values may be a value type, may be reference type;
named 3. compliance function identifier naming convention, capitalized first letter indicates that the function of this package file may be used, and other package files, the first letter lowercase only be expressed using the package file;
4. the variables in function local, external function does not take effect;
5. array of basic data types and default values are passed: copying a value, changes do not affect the original value in the function;
6. If you want to modify the default data type in the function, can be transferred the variable & address, function to the pointer variable manner, similar reference;
7. the Go function does not support the function overloading;
8. the Go *** is a function *** a data type can be assigned a function to a variable, the variable may be performed by function calls;
9. can function as a parameter passed to the function, and a call;
10. the function returns the value of the named support;
11. support variable parameter;
main Package 

Import "FMT" 

FUNC Test01 () { 
   var A int 
   fmt.Println (A) // 0 
} 
FUNC Test02 (n-int) { 
   n-10 = + 
   fmt.Println (n-) // 10 
} 
FUNC TEST03 (n-* int) { 
   * = 10 + n- 
   fmt.Println (n-*). 11 // 
} 
FUNC test04 (n-FUNC ()) { 
   n-() 
} 
FUNC for TEST05 (n-int) (int RES) { 
   RES + = n- 
   return 
} 
FUNC test06 (A ... int) { 
   var int COUNT 
   for _, V: Range A = { 
      COUNT = + V 
   } 
   fmt.Println (COUNT) 
} 
FUNC main () { 
   // fmt.Println (A) 
   //. 1. function is local, test01 external function does a variable
   // 2. The basic data types are passed by value, changes do not affect the function of the original value in 
   var int B 
   Test02 (B) 
   fmt.Println (B) // 0 
   // 3. wish to modify the basic data types in the function, can be address variable transmission; 
   var C =. 1 
   TEST03 (& C) 
   fmt.Println (C). 11 // 
   // 4. function is a data type can be assigned to a variable 
   var FUNC D () 
   D = Test01 
   D () / / 0 
   // The function may be passed to the function as parameter 
   test04 (D) // 0 
   // 6. The function returns the value of the named support 
   RES: for TEST05 = (10) // 10 
   fmt.Println (RES) 
   //. 7 the function supports variable parameters 
   test06 (1,2,3,4,5,6) 21 is // 
   // slice may be passed as a parameter of the function, the learning will be detailed later when the slice 
   var f = [] int {1 , 2,3,4,5,6} 
   test06 (F ...) // 21 is 
}

  There are recent articles on personal micro-channel public number, welcome everyone's attention, with the exchange of learning;



Guess you like

Origin www.cnblogs.com/Mail-maomao/p/11411754.html