Go Basic Notes_2_Environment Construction

Chapter 2 Go Environment Construction

1. Download and install

SDK download address

insert image description here
Other versions can be selected as needed, and then installed
insert image description here

2. Environment variable configuration

In windows, add the directory where the installed go is located to the environment variable value of PATH.
Linux can be configured as follows

sudo vi ~/.bash_profile

1. GOPATH

Specify the development workspace, the directory where source code, test files, library static files, and executable files are stored. Don't be the same as the Go installation directory.
The default value of GOPATH in windows is %USERPROFILE%\go.
On linux and macOS, the default value of GOPATH is $home/go.
Linux can be added at the end of the ~/.bash_profile file (the directory name is customized by golang):

export GOPATH=$HOME/golang

2. GOROOT

The installation directory of the Go language. Multiple Go language versions installed can be distinguished through this configuration.

Linux adds the following at the end of the ~/.bash_profile file (the directory name is customized by go15):

export GOROOT=$HOME/go15
export PATH=$PATH:$GOROOT/bin

3. GOBIN

The installation directory of the compiled binary command is generally GOPATH/bin
linux. Add the following at the end of the ~/.bash_profile file:

export GOBIN=$GOPATH/bin

Linux input: wq to save and exit, then enter go env to view

go env

After Windows restarts, open cmd and enter go version to view

go version

3. The indispensable hello world program

package main
import "fmt"
func main(){
    
    
    fmt.Println("Hello World!")
}

run

E:\workspace\GO_PATH\wincmd_space>go run helloworld.go
Hello World!

or compiled into an executable

E:\workspace\GO_PATH\wincmd_space>go build helloworld.go
E:\workspace\GO_PATH\wincmd_space>helloworld.exe
Hello World!

Four. Brief analysis of hello world program

package main
import "fmt"
func main(){
    
    
    fmt.Println("Hello World!")
}

  1. package main
    indicates that this source code file belongs to the main package, and the main package is a package included in every Go application, and there is only one.

  2. import "fmt"
    imports a package named "fmt", and then you can use various functions provided by the fmt package.
    Once a package is imported, it must be used, otherwise an error will occur.

  3. The main()
    function is quite special. It is the "entry" function of the Go program and the starting point of the program running.
    This function must exist and only one can exist, and must be declared in the main package.
    Any Go function requires paired curly braces { } to wrap the function body.

  4. Println() function
    The Println() function in the fmt package, the function of this function is to output specific content to the console.

5. Example

package main
import "fmt"
func main(){
    
    
    //fmt.Println("Hello World!")
}

The Println method is commented here, execute go build helloworld.go, the error is as follows

go build helloworld.go
# command-line-arguments
.\helloworld.go:2:8: imported and not used: "fmt"

Therefore, the imported package must be used.

go annotations use

In the Go language, comments include single-line comments and multi-line comments.

  1. Single-line comments
    Single-line comments are represented by //
package main
import "fmt"
func main(){
    
    
    // 这里是一行注释
    //fmt.Println("Hello World!")
}
  1. Multi-line comments
    Multi-line comments are also called block comments, and the format is one or more lines starting with "/*" and ending with "*/".
    Multi-line comments cannot be nested.
/*
入口函数:main()
功能:打印Hello World
 */
package main
import "fmt"
func main(){
    
    
    fmt.Println("Hello World!")
}

Guess you like

Origin blog.csdn.net/u010895512/article/details/124364997