Every programming language begins with a “Hello World” program

Create the helloWorld. Go

In the HOME/go directory (the GOPATH directory), create a directory called SRC, then create a folder called first in that directory, create a file called helloWorld.go in that directory, double-click on it, and enter the following

Import "FMT"; // Import "FMT"; Println("Hello World") // Print Hello World}Copy the code
  • Create a package

The Go language is managed by “packages”, and each GO source file must first declare the package to which it belongs, so we see that each GO source file is a package declaration

The GO language has one-to-one mapping between packages and folders. It has the following features:

  1. Sibling files in a directory belong to the same package
  2. Package names can be different from their directory names
  3. The main package is the entry package for a GO program. A GO program must have only one main package. If a program does not have a main package, it will fail to compile and produce executable files
  • import “fmt”

After the package declaration, the import “FMT” statement is used to import the packages that the program depends on. The imported package names must be enclosed in double quotes

Imported and not used: “XXX”, “XXX” indicates the package name.

It is also possible to import multiple packages using the same import keyword by enclosing the package names in parentheses () and using a single line for each package name, as follows:

import ( name1, name2 )
Copy the code
  1. Point of operation
import (
    . "fmt"
)
Copy the code

The meaning of this dot operation is that after the package is imported, when you call a function on the package, you can omit the package name prefix, which is what you called before

Println(“hello world”) Println(“hello world”)

  1. The alias operation
import (
    f "fmt"
)
Copy the code

For alias operations, the prefix becomes our prefix when calling the package function, which is f.prinln (” Hello world”).

  1. _ operation
import (
    "database/sql"
    _"github.com/xxx"
)
Copy the code

The operation actually introduces the package. Instead of using the functions in the package directly, it calls the init function in the package

  • The main function

The main function is one of the custom functions. In Go, all functions begin with the keyword func

It is the entry function of the Go language program, which is the first function to run after the program is started. The main function can only be declared in the main package, not in other packages, and there must be only one main function in a main package.

Execute the GO program

A:

  1. Open a terminal
    • In the window window, use the shortcut key Win +R and enter CMD to open the command line prompt
    • On Linux, you can use the shortcut keys CTRL + Alt +T
    • In the MAC command box, enter termainl
  2. Step2: go to the directory where helloworld.go is located
  3. Step3: run the go run helloworld.go command and observe the result.

Method 2:

  • Open the terminal, run in any file pathgo install hello
  • You can also enter the project path and rungo install

Run the go program under /home/go/bin/(which is automatically created if there is no bin directory before) and you will see that a hello executable file appears. Run the following command:./hello

The last

Just learn Golang, hope everyone supervise and work together

Interested partners, welcome to follow my subscription number: EntrepreneurialG