Golang- functions, packages

The basic concept of functions

  As a function of a set of complete program instruction (statement), the function is called.

 

  In Go, the function is divided into: a custom function, system function ( see Go Programming Guide)

The basic syntax for function

  

 

 

 Quick Start Case

   

 

 

 

package

  Package leads

    1) In the actual development, we often need different files, to call a function defined in other files, such as main.go in, to use the function utils.go file, how to achieve? -"package

    2) There are two programmers to develop a Go program, the programmer xiaoming want to define a function Cal, programmers xiaoqiang also want to define a function, also called Cal. For this purpose two programmers also had a falling out, how do -? "Package

  Schematic package

    In fact, the essence of the package is to create a different folder to store the program files. Paint explain the principles of the package

     

 

 

   The basic concept of packages

    Description: go every file is part of a package, that package is in the form go to manage file and directory structure of the project

 

   Three major roles package

    Distinguishing the same function name, variable identifiers, etc.

    When a lot of program files, you can well managed project

    Control functions, variables, etc. access scope, i.e. the scope

  Instructions package
    packetized elementary grammar
    package package name

  The basic syntax introduced package
    import "package path"

  Quick Start package used

    -Go pack Quick Start function call each other, we will define the func Cal to file utils.go, will utils.go into a bag, when other files need to utils.go way, you can import the package, you can used.

    [To illustrate: the new project directory structure]

    

    utils.go file

    

 

 

     main.go file

    

  Notes and details of the package used to discuss
    1) when given a file package, the package corresponds to a folder, such as where the utils folder that corresponds to the package name is utils, package name usually files of the same folder names consistent , usually lowercase letters.
    2) When a file to use functions or variables other packages, the corresponding packet is introduced need
      of adding methods 1: import "Package Name"
      is introduced mode 2:
      Import (
        "package name"
        "Package Name"
      )
      Package first instruction file line, then the import command.
      When import package, the path from the next src $ GOPATH, do not bring src, the compiler will automatically begin to introduce from next src
    3) In order to allow the file to other packages, you can access the functions in this package, then first the function name capitalize letters, similar public other languages, so as to cross-package visit. For example, the utils.go

      

 

 

     4) when accessing other packet functions, variables, the syntax is the package name. Function name, such as file here main.go

      

 

 

     5) If the package long name, Go to the support package alias, attention to detail: the alias, the original package name can not be used

       

 

       Note: If the alias to take the package, you need to use an alias functions and variables to access the package.   

    6) In the same package, can not have the same function name (can not have the same global variable name), otherwise defined newspaper repeated
    7) If you want to compile into a single executable file, you need to declare this package is main, that package main. this is a syntax specification, if you write a library, you can customize the package name

      

 

 

Function call mechanism of
  understanding of user-friendly way

  

 

   Function - calling process

    Description: To give you a better understanding of the function call, look at the two cases, and draw the schematic, this is very important to
    1) pass a number +1

        

      Of the figure shows
      (1) when calling a function, the function will allocate a new space, the compiler will make this process through its own new space and other space stack distinguish

      (2) in the stack corresponding to each function, the data space is an independent, not confuse
      (3) When a function call is completed (finished), the program will destroy the function corresponding stack space.
    2) calculating two numbers, and returns

       

 

   

  return statement

     The basic syntax and description

      

 

     

    Case Presentation 1
    please write to give the function, you can calculate the sum and difference of two numbers and returns the result.

     

 

     Case Presentation 2

    A detailed explanation: want to ignore a return value, the _ notation occupying ignored

    

 

Notes and detail functions discussed using
  1) the parameter list may be a plurality of functions, the returned list may be plural.
  2) the data parameter lists and return types of value may be a list of types and reference types.
  3) follow the naming function identifier naming convention, the first letter is not a number, the initials of this function can be used, and other package file package file, similar to public, the first letter in lower case, this package file can only be used, other packages files can not be used, similar privat
  function of variable 4) is local, external function does not take effect [Remarks]

    

 

 

   5) an array of basic data types and default values ​​are transmitted, i.e., a value copied. Modified within the function will not affect the original value.

    

 

 

   6) If desired function can be to modify variables in function of the external variables (refers to the default value so as to transfer the data type), the address of the variable & can pass, the function pointer in a variable manner. In effect similar references.

     

 

 

   7) Go function does not support function overloading

    

 

 

   8) in Go, the function is a data type can be assigned to a variable, the variable is a function of the type of variable. You can call a function through the variable

     

 

 

   9) Since it is a function of a data type, and therefore in Go, as a function parameter, and call

      

  10) In order to simplify data type definitions, Go supports custom data types
  basic syntax: type-defined data type name Data Type // understand: the equivalent of an alias Case: type myInt int // int this time myInt is equivalent to using .int

  Case: type mySum func (int, int) int // Then mySum is equivalent function of a type func (int, int)

   Illustrates the use of custom data types:

      

 

 

   11) Support the return value of the function name

       

 

 

   12) Use _ identifier, the return value is ignored

    

 

 

   13) Go to support variable arguments

    

 

 

     (3) If there is a variable parameter the parameter list of the function, the variable parameters need to be placed last parameter list. Code demonstrates:

        

 

 

 

Classroom exercises functions

  1 title

  

 

   Question 2

  

 

   Problem 3: Please write a function swap (n1 * int, n2 * int) can exchange value n1 and n2

  

 

 

 

 

  

 

Guess you like

Origin www.cnblogs.com/Essaycode/p/12642113.html