Getting Started with Quick Development in Go
Develop a hello.go program
package main
import "fmt"
func main() {
fmt.Print("hello")
}
输出:
hello
Explanation of the above figure
- The suffix of the go file is .go
- package main indicates that the package where the hello.go file is located is main, and in go, each file must belong to a package.
- import "fmt" means: import a package, the package name is fmt, after importing the package, you can use the functions of the fmt package
- For example: fmt.Println
- func main() { }
- func is a keyword indicating a function.
- main is the function name, which is a main function, which is the entry point of our program.
- fmt.Println("hello") means calling the function Println of the fmt package to output hello, world
Golang execution process analysis
-
If the source code is compiled and then executed, the execution flow of Go is as follows
-
If we directly execute the go run source code on the source code, the execution flow of Go is as follows
The difference between the two ways of executing the process:
- If we first compile and generate an executable file, then we can copy the executable file to a machine without a go development environment, and it can still run
- If we directly go run the go source code, then if we want to run it on another machine, we also need a go development environment, otherwise it cannot be executed.
- When compiling, the compiler will include the library files that the program depends on in the executable file, so the executable file becomes much larger.
Compile and run instructions
-
With the go source file, compile it into a binary code file that the machine can recognize through the compiler.
-
In the source file directory, compile the hello.go file through go build. The generated executable file name can be specified
-
It must be .exe suffix under windows, and an executable file under Linux
-
If there is an error in the program, an error will be reported on the wrong line when compiling. Helps programmers debug errors
Notes on Go program development
-
Go source files have a "go" extension.
-
The execution entry point of a Go application is the main() function.
-
The Go language is strictly case-sensitive.
-
A Go method is composed of one statement after another, and no semicolon is required after each statement (Go language will automatically add a semicolon after each line)
-
The Go compiler compiles line by line, so we write one statement per line, and we cannot write multiple statements in the same line, otherwise an error will be reported
-
If the variables defined by the Go language or the imported packages are not used, the code cannot be compiled.
- Use the goland development tool, if it is not applicable, it will be automatically deleted
-
If the variables defined by the go language or the imported packages are not used, the code cannot be compiled
-
Curly braces appear in pairs and are indispensable.
note
- The text used to annotate and explain the program is a comment, which improves the readability of the code;
- Comments are a good programming habit that a programmer must have.
- Sort out your own thoughts through comments first, and then use code to reflect them.