Golang environment configuration (detailed)

Golang environment configuration

System hardware and operating system:

64-bit Windows operating system, processor Intel® Core™ i7-8750 CPU @ 2.20GHz

Installation process

Install VSCode editor

Simply go to the official website to download VSCode, and follow the installation prompts to complete the installation.

The benefits of running Golang language on VSCode
Visual Studio Code is a lightweight but powerful source code editor that can run on Windows, macOS and Linux desktops. It has built-in support for JavaScript, TypeScript and Node.js, and provides rich extensions for other languages ​​(such as C++, C#, Java, Python, PHP, Go) and runtimes (such as .NET and Unity) ecosystem.

Install Golang

The download can be completed on the Golang official website, link: link

In accordance with the installation was successful, enter cmdopen a command prompt, and then enter the go envcommand language go to detect whether the installation is successful, if a successful installation will appear as shown below:

Install VSCode plugin

In the extended application store of VScode, search for go and install it to successfully install the plug-

in. Problems: After installation, if you want to successfully compile and run, you also need to download golang dependency packages. These dependency packages often fail to download. The reason may be caused by the instability of the network (the external network will also fail).

Solution: by cmdopening a command line and then perform go env -w GOPROXY=https://goproxy.cn,directwill switch to seven cattle cloud agent, re-opened after switching VSCode a good agent, Installing all clicks in the prompt can be successfully installed

VSCode runs hello world

Create a folder on the local computer to store the golang project, and then create the hello.go file, the code is as follows:

package main

import "fmt"

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

VSCode the press F5can successfully compile and run, results are as follows:

Goland runs hello world

According to some information on the Internet, many users recommend the Goland software to run the go file. I also downloaded and installed it. Finally, through the configuration environment, I successfully completed running the hello.go file created above.

Install and run go tour

Successfully install the gotour file into the bin folder of your workspace through the following command, and run successfully

$ go get github.com/Go-zh/tour/gotour
$ gotour

My first package and test

Create a reverse.go file in your working folder, the content is as follows:

// stringutil 包含有用于处理字符串的工具函数。
package stringutil

// Reverse 将其实参字符串以符文为单位左右反转。
func Reverse(s string) string {
    
    
	r := []rune(s)
	for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
    
    
		r[i], r[j] = r[j], r[i]
	}
	return string(r)
}

Put the file in the stringutil folder, and put this folder in the src folder, so that a package is successfully created, and then import the previous hello.go file into this package and test it. The test results are as follows :

From the result of the above figure, we can see that the stringutil package was successfully created

Summary of issues and points

  • Question 1: When configuring the golang environment in VSCode, the dependency package download fails

    You can change the proxy through the command line to quickly download the dependent package

  • Question 2: How to create a package in Windows operating system

    Need to find the original installation directory, have the corresponding bin, src, pkg folders, put the newly created package into the bin folder, and be able to successfully compile without error (GOPATH should also be this folder), and then create a new File, you can import the created package

Summary: Through this configuration of the Golang environment, I have learned the basic operations of VSCode and Goland, and know how to compile and run go files, as well as simple debugging. Through the writing of this experimental report, I have gradually mastered the usage of the markdown editor and found its convenience. I will use markdown to write more text in the future.

Guess you like

Origin blog.csdn.net/qq_43267773/article/details/108505156