“This is the 19th day of my participation in the August Gwen Challenge

Summary of the package

  • Packages are organizational units in the Go language. Packages are logical groupings. The physical grouping is different folders, and folders and packages generally correspond
  • If you put multiple files in the same folder, they are in the same package.
  • Although the package and folder name of the source file are allowed to be different, the package of the file will be compiled into the folder name after the final compilation. Therefore, to prevent errors, it is best to set the package and folder name of the file to be the same
  • A Go project must have a main package, and any number of other packages with custom names can be as many as you want.
  • Go looks for packages in the GOPATH/ SRC path, or if they don’t exist, Go to GOROOT/ SRC (the Go library source folder)
  • Resources in different packages can access each other. After other packages are imported, you can access the contents in the upper case of each package
  • Global resources in different files under the same package can be accessed at will

The custom package

  • After creating a project, create a SRC folder under the project and create a demo file in the SRC folder
  • Create demo1.go and demo2.go files in the demo file
  • The source code of demo1.go file is as follows
package demo/ / package for the demo

import "fmt"

func demo1(a){
	fmt.Println("Executive not")}Copy the code
  • The source of the demo2.go file is as follows
package demo/ / package for the demo

import "fmt"

func Demo2(a)  {// The function name must be uppercase to be accessed by other packages
	fmt.Println("Executive demo2")
	demo1()// Access the contents of the same package
}
Copy the code
  • Create main.go in the project root directory. The source code is as follows
package main

import "demo"

func main(a) {
	demo.Demo2()
}
Copy the code
  • After running the entire project, you find that the Demo2() function can be called normally
  • The entire program directory structure is as follows

Variable scope

  • The location of the variable declaration determines how accessible the variable is (where it can be called)
  • The valid range of variables in Go is as follows
    • Function level: Variables are declared inside a function and can only be accessed inside the function. Variables are called local variables
    • Package Level, accessible under the current package. Call variables global variables. Variables are declared outside the function
    • Application level, accessible within any package within the entire application. Controlled by initial case

A local variable

  • Local variables must be inside the function
  • Which {} is declared inside, which {} can only be accessed inside
func test1(a) {
	i := 2 I can be called anywhere from here to the end of test1
	if i>=2{
		j:=3
		fmt.Println(i+j)// I can be accessed here
	}
	fmt.Println(i)
	// fmt.println (j)// cannot call j here, beyond {} when declaring j
}
func test2(a) {
	fmt.Println(i) Test1 () cannot be called here
}
Copy the code

The global variable

  • Global variables are declared outside the function and the entire package is accessible
  • Cross-package access is also possible if the global variable starts with a capital letter.
  • The specification for declaring global variables is
var(Variable name Variable name = value)Copy the code
  • Global variable code examples
var (
	name = "smallming"
	age  = 17
)

func demo1(a) {
  	fmt.Println("Name:",name)
}

func demo2(a) {
  	fmt.Println("Age.",age)
}
Copy the code

Summary of the closure

  • Closures are not unique to the Go language; they are found in many programming languages
  • Closures are a solution to the problem that local variables cannot be accessed externally
  • Is an application of a function as a return value

Code demo

  • The general idea is: define a local variable inside a function and treat another function as a return value. The local variable is equivalent to the global variable for the return value function, so the value of the local variable changes with multiple calls to the return value function
package main

import "fmt"

func main(a) {
	//res is a function that returns a value from test1
	res := test1()
	fmt.Println(res()) 2 / / output
	fmt.Println(res()) 3 / / output
	fmt.Println(res()) / / output 4
}

// Note here that the return value type is func int
func test1(a) func(a) int {
	i := 1
	return func(a) int {
		i = i + 1
		return i
	}
}
Copy the code
  • Recalling test1() redeclares and assigns the local variable I
package main

import "fmt"

func main(a) {
	f := test1()
	fmt.Println("Address of F", f) // Prints the address of the anonymous function
	fmt.Println("f:", f()) // Call anonymous function output 2
	fmt.Println("f:", f()) // Call anonymous function output 3
	k := test1()
	fmt.Println("K's address", k) // Outputs the address of the anonymous function, the same as f
	fmt.Println("k:", k()) // Call anonymous function output 2
	fmt.Println("f:", f()) / / output: 4
	fmt.Println("k:", k()) / / output: 3
}

func test1(a) func(a) int {
	i := 1
	return func(a) int {
		i++
		// Each time test1() is called, the output address is different
		fmt.Println("Address of I :", &i)
		return i
	}
}

Copy the code