This is the 9th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021
Hello, everyone, I am my old cousin. Recently, I have an idea to learn the Go language, and also incidentally open the Go language learning column. I hope I can write some helpful content for other beginners or readers who have questions.
Go is an open source programming language that makes it easy to build simple, reliable, and efficient software with the following features:
- Can be directly compiled into machine code, strong performance
- Support concurrency, garbage collection
- The standard libraries are large, small and efficient to develop
- Simple code, unified format, easy to read
Installation environment
Go to the go official website to download the installation package. Here we can download the Installer version from the stable version. Official website address:golang.google.cn/dl/ Compared with the decompression version, the direct installation package installation can save their own configuration of environment variables and other operations, fool one-key installation, convenient, suitable for beginners.Once the installation is complete, we can open the terminal to see if GO is installed. Typego version
You can test whether the go command is working properly and view the installed VERSION of Go.If the version information is displayed, the go environment is successfully installed
Test use: Hello World
Create a directory called go to store the code related to the go language. Create a new hello.go directory.
cd Desktop/Project/ && mkdir go
touch hello.go
Copy the code
Open it directly in a text editor, copy in the following code, save and close, and you have written your first project code for go.
package main
import "fmt"
func main(a) {
/* The programmer's first line of code */
fmt.Println("Hello, World!")}Copy the code
Open the terminal, enter the corresponding directory, and execute the go run run code:
cd Desktop/Project/go
go run hello.go
Copy the code
Let’s take a quick look at what these lines of code do:
// Indicates that the current hello_go.go package is main
package main
// Import a package, like in Python
import "fmt"
Function name (){function content} main is the program execution entry */
func main(a) {
// The programmer's first line of code
// Call Println in the FMT package to print the string
fmt.Println("Hello, World!")}Copy the code
Now that we’ve installed and simply written a project using Go, we’ll learn more.
Install & use VS Code
In order to make it easier to learn and write code, it is necessary to install a good development tool. I checked it on the Internet and finally chose VS Code. The main reason is that vs Code is open source and free, and there are many plug-ins.
Base installation
Official website download address:code.visualstudio.com/ Select the system and download the stable version.
Download good, unzip after the direct click can open the use, do not install, can also install Chinese language package, direct interface Chinese, use more convenient.
Loved it so much, installed a lot of toolkits:
shortcuts
Ctrl/Command+P to quickly bring up the query window to find files, Ctrl/Command+Shift+P to quickly bring up the Command window to do things like snippets, Set shortcut code block Ctrl/Command+Shift+M Quickly display "Problem" panel Ctrl/Command+H Find replace Ctrl/Command+Shift+F Find in the entire folder Ctrl + ~ Bring up terminal or hideCopy the code
- Set the shortcut code block
Ctrl/Command+Shift+P in VS Code, then typesnippets
After clicking in, select the language in which you want to add the snippet, such as Go.After entering the configuration file, a comment is displayed
{
// Place your snippets for go here. Each snippet is defined under a snippet name and has a prefix, body and
// description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the
// same ids are connected.
// Example:
// "Print to console": {
// "prefix": "log",
// "body": [
// "console.log('$1');" .
/ / "$2"
/ /,
// "description": "Log output to console"
// }
}
Copy the code
Enter a dictionary (json string) as prompted by the comment, as shown in the following example using PLN to extend FMT.Println($0).
"println": {"prefix": "pln", # triggers the code block to take effect"body":"fmt.Println($0)", # extend code block content"description": "println"# comments}Copy the code
Create a new GO project in VS Code
directlyFile -> New file
It creates a new code file, not a folder or initialization project, and the file can be selected by its own programming language or identified by the program itself.So here we chooseFile-open
And then toDesktop/Project/go
Create a new one below the directory we used to store the GO projecthello_go
Folder to create a new project in this way.
After creating the project file, create a new 01_HELLO_go. go file in the folder and copy and paste the hello_world code into it.Ctrl+F5 can be run automatically or run in run-undebug mode from the function bar.The first run will prompt you to install the relevant toolkit, before installing the program, you need to set up the Go domestic accelerated image, first open the terminal/CMD, enter the following code:
# mac
export GO111MODULE=on
export GOPROXY=https://goproxy.io,direct
# windows
$env:GO111MODULE = "on"
$env:GOPROXY = "https://goproxy.io,direct"
Copy the code
Then update the go toolkit in vscode. If you can’t find the install key, run the code again or press Ctrl/Command+Shift+P and typeupdate tools
Go will do./usr/local/go/bin = /usr/local/go/bin = /usr/local/go/bin = /usr/local/go/bin = /usr/local/go/bin = /usr/local/go/bin = /usr/local/go/bin
sudo chmod -R 777 /usr/local/go/bin
Copy the code
If it still doesn’t work, it’s too hard, and you have to install it manually. Here’s how:
Step 1: Download the relevant toolkit below all git addresses I have changed to domestic can access the fast address, hope to help you
Enter the go environment and create the src/golang.org/x directory
cd $GOPATH
mkdir -p src/golang.org/x && cd src/golang.org/x
Download the necessary add-on kits from Github
git clone https://hub.fastgit.org/golang/tools.git
git clone https://hub.fastgit.org/golang/lint.git
git clone https://hub.fastgit.org/golang/mod.git
git clone https://hub.fastgit.org/golang/xerrors.git
Copy the code
Step 2: Install the tool kit
The tools kit needs to be installed in the tools/ CMD directory, or updated with @latest. Such as:
cd $GOPATH/src/golang.org/x/tools/cmd
go install golang.org/x/tools/cmd/guru
go install golang.org/x/tools/cmd/gorename
Copy the code
cd $GOPATH
mkdir -p src/github.com && cd src/github.com
git clone https://hub.fastgit.org/stamblerre/gocode.git stamblerre/gocode
git clone https://hub.fastgit.org/acroca/go-symbols.git acroca/go-symbols
git clone https://hub.fastgit.org/ramya-rao-a/go-outline.git ramya-rao-a/go-outline
git clone https://hub.fastgit.org/mdempsky/gocode.git mdempsky/gocode
git clone https://hub.fastgit.org/go-delve/delve.git go-delve/delve
git clone https://hub.fastgit.org/uudashr/gopkgs.git uudashr/gopkgs
Copy the code
The latest version of the installation package needs to be updated. If you are not sure, you can add @latest to the package
# indicates that the latest version is installed
cd $GOPATH
go install github.com/stamblerre/gocode@latest
go install github.com/ramya-rao-a/go-outline@latest
go install github.com/acroca/go-symbols@latest
go install github.com/mdempsky/gocode@latest
go install github.com/go-delve/delve/cmd/dlv@latest
Copy the code
If you are still missing something, git download it and install it.
(Already said in front, here again sigh, this problem card I 1 day time!!) Before so many manual operations, to the last found is their own directory permissions cause the installation of the problem, directly modify the permissions can be installed successfully, oh!
sudo chmod -R 777 /usr/local/go/bin
Copy the code
After the dependency packages are installed, we can run the code again to successfully outputHello World
~
See you next time. I’m a cat lover and a tech lover. If you find this article helpful, please like, comment and follow me!
Refer to the article
[A] Why use Go? What are the advantages of Go? : www.zhihu.com/question/21…
[b] Mac system under the language environment of installation and configuration: bbs.huaweicloud.com/blogs/detai…
[C] Is there a good IDE for Go? www.zhihu.com/question/25…
[d] what are the advantages and disadvantages of Atom, Sublime Text and VSCode? www.zhihu.com/question/41…
[e] LiteIDE X and VSCode www.reddit.com/r/golang/co…
[f] VS Code configuration Go language development environment www.liwenzhou.com/posts/Go/00…
[e] macos go language and vscode plug-in installation blog.csdn.net/luxingjyp/a…
[f] vscode golang plug-in installation, error permission denied www.cnblogs.com/fsqsec/p/14…