The article is constantly updated every week, and it is not easy to be original. It is the biggest affirmation for me to let more people see “Three companies”. You can search the public account “Back-end Technology School” on wechat to read it for the first time (usually one or two posts are updated earlier than the blog).

Over the past few weeks, I have written a series of articles on back-end technologies, including databases, microservices, memory management, etc. I prefer to learn in a systematic way, so there will be a series of articles on databases and microservices.

Recently, I have been working on Golang programming. Now many Internet companies are turning to Golang development, so I plan to write a series of articles about Go language learning. The goal is to output a series of articles from the Go foundation to the advanced level, precipitation these knowledge and also for everyone’s reference, and strive to be easy to understand. Even if you are a Golang, you can understand it. If you are a veteran, you can learn something new.

This article will share with you how to install Golang on Linux and how to remotely develop and debug Golang programs by configuring VSCode.

Download the source code

You can install Golang with built-in package management tools such as yum or apt-get. However, for the sake of universality, I choose to install and explain through the source code, download the source code from the official website, download the address golang.org/dl/

Manual installation

Unpack the installation

Here I download the source package go1.14.2.linux-amd64.tar.gz to the remote Linux server directory. Run the following command to install to the /usr/local directory.

tar -zxvf -C /usr/local/ ` go1.14.2. Linux - amd64. Tar. Gz `Copy the code

Creating a workspace

A workspace is the “working directory” for your Go project. Pick an appropriate directory and do the following:

mkdir GoPath
mkdir -p GoPath/src
mkdir -p GoPath/bin
mkdir -p GoPath/pkg
Copy the code

Three directory meanings:

  src: source path (for example:.go,.c,.h,.sEtc.)pkg: Generated when the package is compiled.aFile Storage Pathbin: Path to the compiled executable fileCopy the code

Configuring environment Variables

There are several environment variables that need to be configured during the installation process.

The GOROOT: Go installation path, which is the /usr/local/go directory we extracted earlier.

GOBIN: directory for storing binary files of the Go project.

GOPATH: Go workspace. The workspace directory was introduced earlier.

Add the following content to the /etc/profile file to complete the configuration.

Export GOROOT=/usr/local/go export GOPATH=/yourpath/ GOPATH # Set your own GOPATH path export GOBIN=$GOPATH/bin export PATH=$PATH:$GOROOT/bin # Add the PATH environment variable export PATH=$PATH:$GOPATH/binCopy the code
# source /etc/profile # Effective immediately
Copy the code

Verify the installation

# go version  # check version
#Go version go1.14.2 Linux/amd64Print the version number
Copy the code

If you see the version information, the installation is successful!

Remote development

We have installed Golang development environment in Linux, but I don’t want to log in to the server to write debugging program every time I open the terminal. How can I develop and debug Golang program on the local PC?

For those of you who have read my previous article on remote development with Vscode, we are going to build Golang remote development environment with Vscode. Specific remote development configurations can be found in another of my articles.

Golang development plugin

First, install the official recommended Go development plugin, as shown below.

The following prompt will appear, because of the lack of other Go development related plug-ins, click Install all to install them all.

Hello World

There is a convention in programming that learning any language starts with Hello World. Now let’s write our first HelloWorld program in Golang.

The code:

package main // All Go programs run from the main package

import "fmt" // Import FMT package

func main(a) {
	fmt.Print("hello world"." i am ready to go :)\n")
	fmt.Println("hello world"."i am ready to go :)")}Copy the code

Formatting package

FMT is similar to C++/C language format IO library functions.

Both Print and Println can be used to Print out output, but their functions are slightly different. As you can see, I added a space and a newline to the last string in the Print function, so that the two printed results are the same.

Print

func Print(a ... interface{}) (n int, err error)Copy the code

Print formats its arguments in the default format and writes them to standard output. If two adjacent arguments are not strings, Spaces are added between their output. Returns the number of bytes written and any errors encountered.

Println

func Println(a ... interface{}) (n int, err error)Copy the code

Println formats its arguments in the default format and writes them to standard output. Always add a space between the output of adjacent arguments and a newline character after the output. Returns the number of bytes written and any errors encountered.

debugging

Terminal debugging

Enter go Run in the terminal command line source directory to run the program.


# go run HelloWorld.go Hello world I am ready to go :) hello world I am ready to go:Copy the code

You can also compile the Go build to get the executable and run it.

# go build HelloWorld.go 
# ls
HelloWorld  HelloWorld.go
# ./HelloWorld 
hello world i am ready to go :)
hello world i am ready to go :)
Copy the code

Vscode debugging

Press F5 to start debugging, edit and debug console output is as follows:

Command line parameters

Command line arguments can be obtained from the OS package’s Args function as follows:

package main

import (
	"fmt"
	"os"
	"strconv"
)

func main(a) {
	// the first argument to os.args is the program itself
	fmt.Println(os.Args)
	for idx, args := range os.Args {
		fmt.Println("Parameters"+strconv.Itoa(idx)+":", args)
	}
}
Copy the code

Terminal Settings

Here is the golang program and output with the argument argv1 argv2.

# go run basic.go argv1 argv2 

#The output[/ TMP/go - build441686724 / b001 / exe/basic argv1 argv2] parameter 0: / TMP/go - build441686724 / b001 / exe/basic parameter 1:2 argv1 parameters: argv2Copy the code

VSCode set

The configuration of the args property of the launch.json file can set the parameters for the program to start debugging.

Once set, press F5 to start debugging and the configured parameters will be printed in the debug console.

Environment variable acquisition

Command line arguments can be obtained from the Getenv function of the OS package as follows:

package main

import (
	"fmt"
	"os"
)

func main(a) {
	// Get environment variables
	fmt.Println(os.Getenv("type"), os.Getenv("name"), os.Getenv("GOROOT"))}Copy the code

VSCode sets the environment variables

Json file args property configuration can set VSCode debugging Golang program environment variables.

The format is: name:vaule, which is a string.

The terminal sets environment variables

The terminal environment variables can be set using the Linux export command, which can then be read using the os.getenv function.

Take, for example, our initial command to set the GOROOT environment variable

export GOROOT=/usr/local/go

Os.getenv (“GOROOT”) is easy to read, but I won’t go into it here.

conclusion

Now that you have an environment where you can develop and debug Golang remotely, go ahead and write Hello World. That’s the end of today’s sharing. The next article will cover basic syntax.

As always, thank you for your reading. The purpose of this article is to share your understanding of knowledge. I will repeatedly verify technical articles to ensure their accuracy to the greatest extent. That’s all for today’s technology sharing, and I’ll see you next time.

It is not easy to be original, so I don’t want to get a blank ticket. If you get something from me, you can move your finger to “like” and “forward”, which is the biggest support for my continuous creation.

You can search the public number “backend Technology School” on wechat to reply to “information” and “1024” have all kinds of programming learning materials I prepared for you. This article is updated every week, and we’ll see you next time!