Go core development study notes (XII) - the basic concept and the concept of function package, the project directory

function

  1. As a function of the complete set of program instructions, called a function.
  2. To solve the problem: code redundancy; not conducive to code maintenance.
  3. Allows main () concise code segment structure.
  4. Custom functions are divided into functions and system functions.

The basic syntax

FUNC <function name> (<parameter list>) (the returned list) {
<executing statements>
return the returned list // return two functions, return values, and termination functions
}

  1. Parameter list represents the input shape function parameters
  2. Function can return a value, you do not.
    Simple Function Example:
    package main
    
    import "fmt"
    
    func calc (n1 float64 , n2 float64) float64 {
    	res := n1 * n2
    	return res
    }
    
    func main() {
    	a,b := 10.0,40.0
    	res := calc(a,b)
    	fmt.Println(res)
    }
    

Role package

  1. Distinguishing the same function name, variable identifiers, etc., each pack is a separate namespace.
  2. When a lot of program files, can be a good project management.
  3. The control function, the variable range of access, i.e. scope.
  4. Introducing package Usage: Import <package path> .
  5. ★★★ function using methods other packages: <package name> <function name> , remember to pack introduced the first letter must be uppercase function name can only be referenced.
  6. May be not only a function of the other package, the package may reference other variables, use: <package name> <variable name> .
  7. If the package name is too long introduction, it may be used as long packet mode alias named after a word, for example: import (util "aa / bb / cc / dd / ee"), this is a function of the subsequent use util <function>. util. <variable>.
  8. The same package can not duplicate the function name.
  9. If you want to compile an executable file, this package must be declared for the main, there is only one package main entrance is the program, other libraries may use a different package name.

About the GOPATH and Goland pit

  1. In Goland the first set Gopath, File-> Setting-> Go-> GOPATH-> Porject Gopath set to the current Gopath, because there will be a lot of projects, so set Gopath current project.
  2. Gopath good configuration in the directory bin pkg src three folders.
  3. All subsequent write their own package must be placed in src directory, import in this project can be written directly to the package name, is called the other functions, import path from below src began.
  4. package package name to write their own functions above and to the best folder name under which the src consistent, convenient personal habits can actually different, package .go files under src / shit called xiaoli, then the other .go import functions require the use of xiaoli, instead of shit.
  5. Function name .go files in the package must be capitalized before they can be referenced by other .go file, or will be reported to the red mark inform variables do not have this function.

About Golang project directory

  1. How the project directory structure of the organization, the general language are not specified. Go language but made provisions in this regard, so you can maintain consistency
  2. In general, a project at GOPATH Go, have the following three directories: $ GOPATH1 = {bin pkg src}
  3. src which placed all the source code of which should be only one main packet, is placed inside the package main .go files, can use only the main package go install main generate executable will be placed in the bin folder.
  4. src in addition to other packages in the main pack, which .go file if the go install xxx builds the library file, xxx.a is a binary file, do not have the readability will be placed in the pkg folder.
Published 50 original articles · won praise 18 · views 4017

Guess you like

Origin blog.csdn.net/weixin_41047549/article/details/89670819
Recommended