Vscode Installs go environment

I still couldn’t resist the temptation of VSCode, so I still chose it. However, due to the domestic network environment, most plug-ins still failed to be installed. Even if I got online scientifically, I still failed. But there is a way, the corresponding VSCode go plug-in, download down directly into gopath bin directory can be, the specific operation I do not explain in detail here, direct portal: VSCode build go development environment

The GOHelloWord

Every language you learn begins with HelloWord. We’ll start with the same rule here, first by reviewing the Gopath directory

Goworks // workspace name (gopath directory) -- bin // Golang generated executable file -- PKG // Golang generated intermediate file (for example: .a) When golang compiles the package -- SRC // stores the source code, i.e. the path where the project is storedCopy the code

For individuals, most people use a Github domain plus a Github account. This is what I use here.

Then we create a folder called Hello as a project. Create a main.go file in this directory:

package main

import "fmt"

func main(a) {
	fmt.Println("Hello World.")}Copy the code

go build

Go build means to compile source code into an executable file.

Execute in the hello directory:

go build
Copy the code

Or run the following command in another directory:

go build hello
Copy the code

The Go compiler will look in GOPATH’s SRC directory for the Hello project you want to compile

Note: It is generally recommended to go to the project directory

When the hello package is not found, he goes back to the SRC in your GOROOT directory and then to the SRC in GOPATH directory. We didn’t create a directory for hello under SRC, so we couldn’t find it.

The compiled executable is stored in the current directory where the compilation command was executed. On Windows platforms, the hello.exe executable is found in the current directory.

The hello.exe file can be executed directly at the terminal:

E:\goworks\src\github.com\ha2ryzhang\hello>hello.exe
Hello World.
Copy the code

We can also use the -o parameter to specify the name of the compiled executable.

go build -o heygo.exe
Copy the code

Package management in Go

The naming of the package

The package name of the GO language follows the principle of concisely, lowercase, and the same name as the directory where the go file is located, so that we can reference, write, and quickly locate and find.

package main

import "net/http"

func main(a) {
	http.ListenAndServe("127.0.0.1:8080",handler);
}
Copy the code

Name the package after the domain name

package main

import "debugers.com/utils" 
Copy the code

Use your Github account to name the package

package main

import "github.com/ha2ryzhang/utils"
Copy the code

That’s how you change the name to Github.com.

The main package

Declaring the package name of a GO file as Main tells the GO compiler that this is an executable, and the go compiler tries to compile it into a binary executable.

For example, both C and Java have main(), which is the entry point to a program without which the program cannot be executed.

In go, both the main package and the main() function must be satisfied to compile into an executable file.

Import packages
import "fmt"
import (
	"net/http"
	"fmt"
)
Copy the code

For package names with more than one path, when referencing in code, use the last package name in the full path as the referenced package name, such as NET/HTTP. We use HTTP in the code, not NET.

The compiler uses the two paths we set, plus the relative full path of the import, to look for packages on disk, such as the FMT package we imported. The compiler eventually finds the $GOROOT/ FMT location.

It is worth knowing that for packages, there is a priority. The compiler searches GOROOT first, then GOPATH, and stops searching as soon as it finds them. If none are found, a compilation exception is reported.

Remote package import
import "github.com/ha2ryzhang/xxx"
Copy the code
  1. inGOPATHUnder the search
  2. Go get the download
After the import
package main

import (
	"fmt"
	myfmt "mylib/fmt"
)

func main(a) {
	fmt.Println()
	myfmt.Println()
}

package main
import(_"mylib/fmt"
)
Copy the code

The go mechanism does not allow you to import packages with the same name unless you rename the package, such as myfmt above. You just need to add a different name to the package guide.

Note: Go does not allow you to import packages that are not in use, resulting in a compilation error, but if you must import them, see the third method above. You can use the package’s init() function to do this.

Package init function

The init function

package mysql

import (
	"database/sql"
    _ "github.com/go-sql-driver/mysql"
)

func init(a) {
	sql.Register("mysql", &MySQLDriver{})
}
Copy the code

Since we only want to execute the init method of the mysql package and do not want to use the package, we need to rename the package with _ when importing the package to avoid compilation errors.