1. Features of GO language

  • Go balances the security and efficiency of static languages with the ease of development of dynamic languages.

  • At the same time, the Go language has excellent support for network communication, concurrency and parallel programming to better take advantage of the large number of distributed and multi-core computers.

  • Build speed (compile and link to machine code), typically in a few hundred milliseconds to a few seconds to build a program, get rid of c++ build nightmare.

  • The root cause of the Go language’s adoption of the package model, which speeds up program building with a strict dependency checking mechanism and provides very good scalability.

  • Memory problems (often referred to as memory leaks) have been with C++ developers for a long time, and the designers of the Go language decided that memory management should not be a concern for developers. So while Go executes native code like any other static language, it still runs in a sort of virtual machine for efficient and fast garbage collection (using a simple mark-clean algorithm).

    (1) Demand:

    1. Print Hello World!

(1) The suffix of the go file is go

(2) package main

Indicates that the package in which the hello.go file resides is main. In go, each file must belong to a package.

(3) import “FMT”

Means: introduce a package FMT

(4) func main(){

}

Func is a keyword that represents a function.

Main is the name of the function. It is the main function, the program entry.

2. Run the go build command to compile the go file and generate the. Exec file.

>cd /Users/tjm/goproject/src/gocode/project01/main/
>go build hello.go
Copy the code

Generate a.exec file

3. Run the hello.exec file

4. Go Run allows you to run hello.go directly (similar to executing a script file, but slower than compiling).

5. Go run and go build

  • Go Build generates executable files that can be copied to run on machines without a Go development environment, whereas Go Run requires a Go development environment.
  • The go source code is compiled into an executable file. The compiler will include the library files that the program depends on to run in the executable file. The file size will become much larger.

6. Go build: Specifies the name of the executable to be generated

> go build -o myhello.exe hello.go
Copy the code

(2) Points for attention in GO programming

  • The Go application execution entry starts from Main
  • Strictly case-sensitive
  • The Go method consists of a series of statements that do not need to be followed by a semicolon (==Go automatically adds a semicolon == after each line).
  • Go is compiled line by line, so only one statement can be written on a line
  • == Code cannot be compiled through == (most different from other languages) if variables defined by GO or packages imported are not used.

(3) Escape char

  • \t: TAB

  • \ n: career change

  • ==\r: Enter ==

    A carriage return does not go back to the next line, but goes back to the beginning of the line, overriding the beginning character.

    package main
    
    import "fmt"
    
    func main(a) {
    	fmt.Println("Little bunny, little bunny, white.")}Copy the code

    ! 3.50.15] [screenshots 2021-04-08 afternoon (/ Users/TJM/Library/Application Support/typora – user – images/screenshots 3.50.15 2021-04-08 afternoon. PNG)

  • \ : a \

  • \ “: a”

(4) Code style

The indentation

  1. shift +tab
>gofmt -w main.go
Copy the code

This directive writes the formatted content back to the file, and when the programmer reopens the file, he sees the formatted file.

Correct code style

package main
import "fmt"
func main(a){
	fmt.Println("hello world!")}Copy the code

Incorrect code style (different from c++, Java)

package main
import "fmt"
func main(a)
{// This is not allowed in the Go language
	fmt.Println("hello world!")}Copy the code