1. Installation instructions

Go 1.4 is required to install the Go 1.11 compiler directly in Ubuntu. So if in the use of apt – get the install installed go when refer to the following link: blog.csdn.net/y5492853/ar…

Overseas official website golang.org/dl/

Domestic official website golang.google.cn/dl/

2. Install Go 1.12

  1. Downloading the Installation package
Wget HTTP: / / https://dl.google.com/go/go1.12.4.linux-amd64.tar.gzCopy the code
  1. Decompress to the /usr/local directory
Sudo tar -c /usr/local-xzf go1.12.4.linux-amd64.tar.gzCopy the code
  1. Modifying environment Variables
vi ~/.profile
Copy the code

Add after the file

Export GOROOT="/usr/local/go" // Go installation directory. That is, decompress the specified path export GOPATH=$HOME/gocode // Locally configured Go code directory export GOBIN=$GOPATH/bin // Directory for storing the compiled Go code executable file export PATH=$PATH:$GOPATH:$GOBIN:$GOROOT/bin // Add the Go installation directory to the operating system PATHCopy the code
  1. Save and exit, and execute
source ~/.profile
Copy the code
  1. Restart the system
  2. Check whether the installation is successful
wohu@wohu:~$go Version go Version go1.12.4 Linux/AMD64Copy the code

3. The first Go program

Create a new text file, hello, with the suffix.go, and edit the following

package main    

import "fmt"

func main(a) {
    / * comment * /
   fmt.Println("Hello, World!")}Copy the code

Execute the above code output

$ go run hello.go 
Hello, World!
Copy the code

3.1 Program structure description

  • package main

Define the package name, which package the file belongs to must be specified in the first non-comment line of the source file. Package Main represents a program that can be executed independently, and each Go application contains a package named Main.

  • import “fmt”

Import the FMT package, which implements functions to format IO (input/output).

  • function main

Func main() is the function the program starts executing. The main function is a mandatory part of every executable program, and is generally the first function to be executed after startup (or if init() is available).

  • / _ _ / comment

Single-line comments are the most common form of comment, and you can use single-line comments starting with // anywhere. Multi-line comments, also known as block comments, start with /*_ and end with *_/. They cannot be nested. Multi-line comments are generally used for package documentation or block code snippet comments.

  • fmt.Println(…)

You can print a string to the console with the newline character \n automatically appended at the end

  • Line separators

In the Go program, a line represents the end of a statement. Each statement does not need a semicolon as in other C family languages; Because this is all done automatically by the Go compiler. If you intend to write multiple statements on the same line, they must be used; Artificial differentiation, but we don’t encourage it in real development.

  • identifier

Identifiers are used to name variables, types, and other program entities. An identifier is actually A sequence of one or more letters (A-Z and A-Z) numbers (0-9) and underscores (_), but the first character must be A letter or underscore rather than A number.

When identifier (including constants, variables, fields, type, function, structure, etc.) begin with a capital letter, such as: Group1, then use the identifier of the object can be in the form of external package code used by (client program need the guide into the package), this is known as a (public) in as an object-oriented language derived.

Identifiers that begin with a lowercase letter are not visible outside the package, but they are visible and available throughout the package (like protected in object-oriented languages).

  • The blank space

Variable declarations in Go must be separated by Spaces, for example, var age int

3.2 Go source code features

  • The source program to.goFor the suffix
  • The source program defaults toUTF-8 coding
  • Identifiers are case sensitive
  • The semicolon at the end of the statement can be omitted
  • Functions tofuncAt the beginning of the function body{Must be at the end of the line where the function header resides
  • stringLiteral usage""Double quotation marks;characterliteralruneuse' 'Enclosed in single quotes
  • Call the method used in the package.Click on an accessor, just like Python
  • mainThe package name of the function must bemain
  • Strongly typed statically compiled languages

4. VS Code Configures the Go development environment

The following error occurs when you use VS Code to configure the Go development environment and install the Go plug-in:

Error: Command failed: /usr/local/go/bin/go get -v github.com/ramya-rao-a/go-outline
Copy the code

For various reasons, we cannot use VS Code to install the corresponding plug-in in one click, so we need to manually download the source package of the plug-in to install.

4.1 Installing Plug-in Scripts

Directly use the following script to install, simple and quick.

#! /bin/bash
cd $GOPATH/src;
mkdir github.com;
cd $GOPATH/src/github.com;
mkdir acroca cweill derekparker go-delve josharian karrick mdempsky pkg ramya-rao-a rogpeppe sqs uudashr ;
cd $GOPATH/src/github.com/acroca;
git clone https://github.com/acroca/go-symbols.git;
cd $GOPATH/src/github.com/cweill;
git clone https://github.com/cweill/gotests.git;
cd $GOPATH/src/github.com/derekparker;
git clone https://github.com/derekparker/delve.git;
cd $GOPATH/src/github.com/go-delve;
git clone https://github.com/go-delve/delve.git;
cd $GOPATH/src/github.com/josharian;
git clone https://github.com/josharian/impl.git;
cd $GOPATH/src/github.com/karrick;
git clone https://github.com/karrick/godirwalk.git;
cd $GOPATH/src/github.com/mdempsky;
git clone https://github.com/mdempsky/gocode.git;
cd $GOPATH/src/github.com/pkg;
git clone https://github.com/pkg/errors.git;
cd $GOPATH/src/github.com/ramya-rao-a;
git clone https://github.com/ramya-rao-a/go-outline.git;
cd $GOPATH/src/github.com/rogpeppe;
git clone https://github.com/rogpeppe/godef.git;
cd $GOPATH/src/github.com/sqs;
git clone https://github.com/sqs/goreturns.git;
cd $GOPATH/src/github.com/uudashr;
git clone https://github.com/uudashr/gopkgs.git;

cd $GOPATH/src;
mkdir -p golang.org/x;
cd golang.org/x;
git clone https://github.com/golang/tools.git;
git clone https://github.com/golang/lint.git;
git clone https://github.com/golang/sys.git;	
git clone https://github.com/golang/net.git;	
git clone https://github.com/golang/mod.git;
git clone https://github.com/golang/xerrors.git;
git clone https://github.com/golang/text.git;
git clone https://github.com/golang/crypto.git;


cd $GOPATH/src;
go install github.com/mdempsky/gocode;
go install github.com/uudashr/gopkgs/cmd/gopkgs;
go install github.com/ramya-rao-a/go-outline;
go install github.com/acroca/go-symbols;
go install github.com/rogpeppe/godef;
go install github.com/sqs/goreturns;
go install github.com/derekparker/delve/cmd/dlv;
go install github.com/cweill/gotests;
go install github.com/josharian/impl;
go install golang.org/x/tools/cmd/guru;
go install golang.org/x/tools/cmd/gorename;
go install golang.org/x/lint/golint;
go install golang.org/x/tools/gopls;
Copy the code

VS Code remote development configuration

If you want to use VS Code to remotely connect to the server for development, please refer to the following links for configuration:

In Windows 10, configure VScode for remote development. Ssh-remote (no-secret login) VScode for remote development Failure to install the GO plug-in Analysis and Solution In Windows GO VScode compilation and running method VScode is automatically formatted when saved