The last issue introduced the following contents:

  • Go’s past life
  • The characteristics of the Go
  • Windows environment variable configuration

The Beginning of Everything: Hello World

// File name: helloworld.go
package main

import "fmt"

func main(a) {
	fmt.Println("HELLO, WORLD!")}Copy the code

Use the command line to manipulate the. Go file

  • Run the Go file

Command: go run helloworld.go

Output: HELLO, WORLD!

  • Compile to exe program

Command: go build helloworld.go

This creates a helloWorld.exe file that you can run directly from the command line with the same output as above.

Go’s package concept

Go uses packages to organize code. A package consists of one or more Go files stored in a folder. The file name describes the purpose of the package, and generally the package name is the same as the folder name.

Each source file starts with a package that declares which package the Go file belongs to. When we look at code like fmt.println(‘Hello’), we assume that the FMT in import FMT at the beginning of the source file is the package name, but this is not the case.

The last element after import is the path, the folder directory, not the package name

Take the FMT package provided by Go for example.

The same directory name and package name is just a Go convention, not always.

import "fmt"
fmt.Println("xxx")
Copy the code

$GOROOT/src. FMT = $GOROOT/src. FMT = $GOROOT/src. FMT = $GOROOT/src. FMT = $GOROOT/src. FMT FMT in the second line is the package name. Gc will find the source file of the FMT package in the $GOROOT/ SRC/FMT path.

Packages can be aliased

import m "fmt" 

func main(a) {
    m.Pringln("Hello")}Copy the code

Use an easily recognizable word as an alias that you can use to call functions in the package.

“FMT” is the path, so m is the alias, m is the path or package name?

M is actually the package name, because you can use m to call the function below

Use dot as the alias for the package

import . "fmt"

func main(a) {
    Pringln("Hello")}Copy the code

When using. As an alias for a package, you do not need a prefix to call functions in that package

with_Import packages

import (
     "database/sql"
     _  "github.com/ziutek/mymysql/godrv"
 ) 
Copy the code

Init () is called to initialize the imported package, but does not use any of the package’s other functions. This paragraph is confusing, why do you initialize an import if you don’t use it? To understand this problem, you need to understand the import principle of Go.

  • Recursive package

When a package is imported, if that package also imports other packages, the other packages are first imported, the packet-level constants and variables in those packages are initialized, and init() is executed, and so on. After all the imported packages have been loaded, the package constants and variables in the main package are initialized, followed by the init() function in the main package and finally the main function.

  • Multiple references, one import

The program is initialized and executed from the main package, and if the main package imports other packages, they are imported at compile time. Sometimes a package will be imported by multiple packages at the same time, so it will only be imported once (for example, many packages will import the “FMT” package, but it will only be imported once because there is no need to import multiple packages)

  • use_The meaning of guide package

First, using _ to guide the package executes the init() function in the package and initializes the variables inside. Often these init() functions register engines in their own packages (connection pool registration, etc.). After importing the package once, other packages do not need to initialize the package again, which is convenient for external calls.

Second, Go’s syntax-checking is so strict that if you import a package and don’t use it, you’ll get an error at compile time. It is possible to compile using the _ guide package without using any of the functions in the package. Note that packages imported with _ cannot be used to call functions

import _ "fmt"

/ / complains
func main(a) {
    _.Pringln("Hello")}Copy the code

The difference between the Go and Java package meanings

  • Different granularity

The minimum granularity of Java package guidance is class

// 1. Reference a concrete class
import java.util.list;
// 2. Reference all classes under package
import com.lark.demo.*
Copy the code

The minimum granularity of Go guide package is Package

Struct (class) import "FMT"Copy the code
  • Different meanings of package

A Java package is just a class directory, and a package itself cannot define methods and variables

Go’s package is a collection of related variables, methods, and constructs. Variables and methods can be defined directly on the package.