Go language foundation introduction the first go program interpretation

Time:2021-12-28
catalogue
  • 1. Write HelloWorld
  • 2. Execute go procedures
  • 3. Code interpretation
  • 4. Summary

Go language foundation introduction the first go program interpretation

1. Write HelloWorld

?
1
2
3
4
5
6
7
8
//filename: hello.go
package main
 
import "fmt"
 
func main(){
    fmt.Println("hello world!")
}

2. Execute go procedures

There are several ways to execute go programs

Method 1: use the go run command
Step1: open the terminal:
Step 2: enter hello Go directory
Step 3: enter go run hello Go command and observe the results.

Method 2: use the go build command
[goroot and gopath need to be configured to create folders SRC and hello]
Step1: open the terminal: under any file path, run: go install Hello [Hello means hello folder, not hello. Go]
You can also enter the path of the project (application package) and run: go install
Step 2: run the Go program under F: / gowork / bin (if there is no bin directory before, it will be created automatically). You will find a hello executable file. Run it with the following command:/ hello

Note that when compiling the Go program, go will actually look for the package in two places: the SRC folder under goroot and the SRC folder under gopath. In the package, automatically find the main function of the main package as the program entry, and then compile it.

Go language foundation introduction the first go program interpretation

3. Code interpretation

  1. Go language uses “package” to organize code. There is only one main function under a project. Package main at the beginning of the code indicates which “package” this file belongs to. The package named main is special. It is used to define an independent executable program. [there is only one main function under a project]
  2. Import tells the compiler which packages this source file needs to be poured into. The required packages must be imported accurately. You can’t import less or more. This is different from C language. C language can import unused header files, but go can’t, but it can’t be compiled. [import the package you need to use, and the use of variables is the same. You must use the definition, otherwise the compilation will not pass]
  3. Println means that it will be output and wrapped according to your input format * * [uppercase means public method, lowercase means private method, and the case of variable name is very strict]
  4. Func keyword defines a function.
  5. The main function must be included in every executable program. Generally speaking, it is the first function to execute after startup.
  6. The go language does not need to end a statement with a semicolon. [in fact, there is no semicolon. The compiler will automatically insert the semicolon at the end of the non blank line in the tag stream]
  7. “{” in braces must be on the same line as func. It cannot start on another line. [otherwise, an error will be reported, which is different from C language, and the code style is unified]
  8. The annotation of go language is the same as that of C language. Use / / to annotate one line and / * * / to annotate multiple lines.

4. Summary

  • There is only one main function under a project
  • Import the package you need to use, and the use of variables is the same. You must use the definition, otherwise the compilation will not pass
  • The statement does not actually have a semicolon. The compiler will automatically insert a semicolon at the end of a non blank line in the tag stream
  • ‘{‘ in parentheses must be on the same line as func. You cannot start on another line

The above is the details of the first go program interpretation for the introduction to the basics of go language. For more information about the interpretation of go language programs, please pay attention to other relevant articles of developeppaper!