Before we dive into the basic syntax, functions, and advanced features of the Go language, let’s write the first Go program “Hello World! “To get started, get to know the structure of Go’s simplest program, see what parts Go should contain, where each part is located, and how Go actually works.
We’re still starting with the eternal “Hello,world” program. Hello. Go source code is as follows:
package main
import "fmt"
func main(a) {
// "Hello World!" Go language program
fmt.Println("Hello World!")}Copy the code
Let’s look at the various parts of the above program:
- First line of code
package main
Defines the package name. You must specify which package the file belongs to in the first non-comment line of the source file, as in:package main
.package main
Represents a independently executable program, and each Go application contains a package called Main. - The next line
import fmt
Tell the Go compiler that the program needs to be usedfmt
Functions or other elements in a package,fmt
The package implements functions that format IO (input/output). - The next line
func main()
Is the function (entry function) that the program starts executing.main
Functions are mandatory in every executable program, and are generally the first function to be executed after startup (init() if there is one), as in Javapublic static void main(String[] agrs)
. - The next line
//
Are comments and will be ignored during program execution. Single-line comments are the most common form of comment, and you can use them anywhere//
A one-line comment at the beginning. Multi-line comments, also called block comments, have been used/ *
Begin and end with* /
End and cannot be nested. Multi-line comments are typically used for package documentation or to comment block code snippets. (Same as other languages) - The next line
fmt.Println(...)
You can print a string to the console with the newline character automatically added at the end\n
A newline. usefmt.Print("hello, world\n")
You get the same result.Print
andPrintln
Both functions also support the use of variables, such as:fmt.Println(arr)
. If not specified, they print the variable arr to the console in the default print format, which I’ll cover separatelyfmt
Package related content.
1. Go language structure
As you can see from the hello World program above, a complete Go program structure consists of the following parts:
- Package declaration
- Introduction of package
- function
- variable
- Statement & expression
- annotation
2. Execute the process
Go program source files are files with a. Go suffix. There are two execution processes:
1. Compile the source file into an executable binary file before running it.
- Compile source code:
go build <xx.go>
Command to generate an executable file. - Run executables: Run executable program files directly.
Often used to compile executable binaries on a Go compilation environment and deploy the executable binaries on any machine that does not have a Go compilation environment.
2. Run the go run
command on the source file.
Often used for development environment testing. (Go Run must rely on the Go development build environment)
3. Development matters needing attention
-
The Go source file name extension is Go, for example, hello.go.
-
The execution entry for the Go application is the main() function.
-
Go is strictly case sensitive.
-
The Go method is made up of statements that do not need a semicolon after each statement. End (in fact, Go automatically adds a semicolon to each line), which shows the simplicity of Go.
-
The Go compiler compiles line by line, so we write one statement in a row. We cannot write multiple statements on the same line, or we will get an error.
-
If the Go language defines variables or import packages that are not used, the code will not compile.
-
Braces come in pairs.
-
Braces {cannot be placed on a single line. Such as:
package main import "fmt" func main(a) { // Error, {cannot be a separate line fmt.Println("Hello World!")}Copy the code
-
Go has built-in Gofmt tools that automatically clean up excess code whitespace, align variable names, and convert aligned Spaces to tabs.