Table of Contents
- Go language training online course
- install go
- The process given by GPT-4
- How to correctly configure the debugging related settings of Go in VSCode?
- What should I do if I always fail to install Go language-related tools in Visual Studio Code?
- I have restarted Visual Studio Code, what should I do next to check the logs?
- What should I do if I still cannot successfully run the code in go language?
- If you still encounter problems running the code in VSCode, try running the Go code directly in the system terminal. Navigate to the directory where the code is located and execute what command?
- In your opinion, for go language development, use vscode or goland?
- How to configure the go language development environment in ubuntu?
-
-
- 1. Download and install the Go compiler
- 2. Set environment variables
- 3. Verify the installation
-
Go language training online course
old boys
Prompt the learning progress of the project: https://learningprompt.wiki/docs/chatGPT/tutorial-basics/basic usage scenarios & skills/scenario-1-ask-questions
Install go
Official website: https://golang.google.cn/dl/
Unzip go to /usr/local as an administrator
sudo tar -C /usr/local -xzf go1.15.2.linux-amd64.tar.gz
Check the version of go
go version
[Ubuntu] Ubuntu setting and viewing environment variables
//my configuration root@long-WUJIE-14:/home/long# sudo vim ~/.bashrc # Recommended root@long-WUJIE-14:/home/long# source $HOME/.profile root@long-WUJIE-14:/home/long# go env GO111MODULE="" GOARCH="amd64" GOBIN="" GOCACHE="/root/.cache/go-build" GOENV="/root/.config/go/env" GOEXE="" GOEXPERIMENT="" GOFLAGS="" GOHOSTARCH="amd64" GOHOSTOS="linux" GOINSECURE="" GOMODCACHE="/root/goWorkPlace/pkg/mod" GONOPROXY="" GONOSUMDB="" GOOS="linux" GOPATH="/root/goWorkPlace" GOPRIVATE="" GOPROXY="https://proxy.golang.org,direct" GOROOT="/usr/local/go" GOSUMDB="sum.golang.org" GOTMPDIR="" GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64" GOVCS="" GOVERSION="go1.20.4" GCCGO="gccgo" GOAMD64="v1" AR="ar" CC="gcc" CXX="g++" CGO_ENABLED="1" GOMOD="/dev/null" GOWORK="" CGO_CFLAGS="-O2 -g" CGO_CPPFLAGS="" CGO_CXXFLAGS="-O2 -g" CGO_FFLAGS="-O2 -g" CGO_LDFLAGS="-O2 -g" PKG_CONFIG="pkg-config" GOGCCFLAGS="-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build2549784198=/tmp/go-build -gno-record -gcc-switches" root@long-WUJIE-14:/home/long# mkdir $GOPATH/src/github.com/github-user/hello -p root@long-WUJIE-14:/home/long# touch hello.go root@long-WUJIE-14:/home/long# ls public docs config.yaml Miniconda3-latest-Linux-x86_64.sh Template Download Documents nohup.out video music go note Image Desktop GO-project PycharmProjects unnamed .ipynb coding hello.go snap root@long-WUJIE-14:/home/long# go run hello.go hello world
Here’s the rest: click
The process given by GPT-4
The steps to configure the Go locale are as follows:
-
Install the Go environment
Download the Go language installation package suitable for your operating system from the official website https://golang.org/dl/, and install it according to the instructions provided.
-
Configure the GOPATH environment variable
Set the
GOPATH
environment variable to the directory where you want to create a Go workspace. For example, set it to~/go
(on Unix systems) orC:\Users\YourName\go
(on Windows systems). -
Install VSCode
Visit https://code.visualstudio.com/, select the appropriate installation file according to your operating system and install it.
-
Install the Go plugin
Open VSCode, click the extension button (rectangular icon) on the sidebar, search for
Go
, find the plugin provided byGo Team at Google
and click Install. -
Configuring the Go plugin
After the installation is complete, click
Reload
in the lower right corner to reload VSCode. Then pressCtrl + Shift + P
(Cmd + Shift + P
on Mac), enterGo: Install/Update Tools
, check all tools and clickOK
. -
Create a Go project
- Create a new project folder:
mkdir -p $GOPATH/src/your_project_folder
- Enter the folder:
cd $GOPATH/src/your_project_folder
- Initialize the Git repository:
git init
- Initialize the Go module:
go mod init your_module_name
- Create a new project folder:
-
Open the project in VSCode
- Open the folder you just created:
code .
- Click the file button on the sidebar of VSCode, right-click on the new file icon, and select to create a new Go file in
your_project_folder
(for example:main.go
).
- Open the folder you just created:
-
Writing Go code
In the
main.go
file, write your Go code. For example:package main import "fmt" func main() {<!-- --> fmt.Println("Hello, world!") }
-
Run and debug code
How to correctly configure Go debugging related settings in VSCode?
To properly configure Go’s debug settings in Visual Studio Code (VSCode), follow these steps:
-
Install the Go extension
First, search for and install the Go extension in VSCode’s extension store. -
Install Go language tools
Open the command palette via
Ctrl + Shift + P
orCmd + Shift + P
(Mac), then enterGo: Install/Update Tools
and execute . This will automatically install Go language related tools for you. Please ensure at least the following tools are installed:dlv
,gopls
,gocode
andgo-outline
. -
Create and configure launch.json file
In the project root directory, create a subfolder named
.vscode
(if necessary), and create a newlaunch.json
file in it. Then, add the following to the file:{ "version": "0.2.0", "configurations": [ { "name": "Launch", "type": "go", "request": "launch", "mode": "auto", "remotePath": "", "port": 2345, "host": "127.0.0.1", "program": "${workspaceFolder}", "env": {}, "args": [], "showLog": true } ] }
Here is a brief description of some key fields:
"type"
: Specify the debugger type, here it should be “go”."request"
: Specifies the debugging type, here is “launch”. Select “attach” if you want to attach to an existing process."mode"
: Debug mode, can be set to “auto”, “debug”, “test” or “remote”."program"
: The path of the Go program to be debugged.${workspaceFolder}
indicates the root directory of the current workspace.
-
Start debugging
What if I always fail to install Go language-related tools in Visual Studio Code?
For problems encountered when installing Go language-related tools in Visual Studio Code, you can try the following solutions:
-
Check network connection
Make sure your internet connection is working. Installation may fail in some countries due to network restrictions. You can use a proxy such as a VPN to get around that. -
Set GOPROXY
Setting the correct GOPROXY environment variable can speed up the download process. Open a terminal window and type the following command:
$ go env -w GOPROXY=https://goproxy.io,direct
- Update Go Tools
Make sure you have the latest version of the Go tools installed. Run the following command in a terminal to update the Go tools:
go get -u all
- Manually install extensions
If it cannot be installed automatically in VSCode, you can try to manually install the required extension package. First, enter the following command in the terminal to get the corresponding toolkit:
go get -v github.com/mdempsky/gocode go get -v github.com/uudashr/gopkgs/cmd/gopkgs go get -v github.com/ramya-rao-a/go-outline go get -v github.com/acroca/go-symbols go get -v golang.org/x/tools/cmd/guru go get -v golang.org/x/tools/cmd/gorename go get -v github.com/cweill/gotests/… go get -v github.com/fatih/gomodifytags go get -v github.com/josharian/impl go get -v github.com/davidrjenni/reftools/cmd/fillstruct go get -v github.com/haya14busa/goplay/cmd/goplay go get -v github.com/godoctor/godoctor
-
Restart Visual Studio Code
Close and restart Visual Studio Code. Make sure the Go tools are properly installed. -
Check logs
If the above methods still don’t work…
I have restarted Visual Studio Code, how should I check the logs next?
To check Visual Studio Code’s logs, follow these steps:
- Open Visual Studio Code.
- Click the
View
(View
) tab in the upper left corner. - Select
Output
in the drop-down menu, this will open the output window in the bottom panel (Terminal area). - In the upper right corner of the output window, click the drop-down menu (default “Tasks” or “Tasks”) and select
Log (Window)
orLog (Window)
to view the logs related to VSCode.
You should now be able to view the Visual Studio Code logs. Here you can find detailed records about errors, warnings or other information. If you need to query the logs of a specific plugin, please select the corresponding plugin in the drop-down menu of the output window.
What should I do if I still cannot successfully run the code in go language?
If you have trouble running Go code, try the following:
-
Check the environment configuration: Make sure the Go language is installed correctly and add it to the system path.
Windows:
set GOPATH=C:\projects\go set PATH=%PATH%;%GOPATH%\bin;C:\Go\bin
macOS and Linux:
export GOPATH=$HOME/go export PATH=$PATH:$GOPATH/bin:/usr/local/go/bin
-
Install and configure the Go plugin:
- Open Visual Studio Code.
- Use the keyboard shortcut
Ctrl + Shift + X
or click the Extensions icon on the left sidebar to open the Extensions panel. - Type “Go” into the search box and find the plugin called “Go” by “go-team at Google” and click the Install button.
- After the installation is complete, restart Visual Studio Code.
-
Update Go plugins and dependent tools:
- Open the command panel of VSCode, use the shortcut key
Ctrl + Shift + P
orCmd + Shift + P
(macOS). - Type “Go: Install/Update Tools” and press Enter to execute the command.
- In the pop-up window, select all listed tools and click on the “OK” or “Confirm” button to start installing or updating these tools.
- Open the command panel of VSCode, use the shortcut key
-
Check code and modules:
- Make sure your Go code is correct and free of syntax errors.
- Check that the
go.mod
file exists and is properly configured.
-
Run the code using the terminal:
If you still encounter problems running the code in VSCode, try running the Go code directly in the system terminal. Navigate to the directory where the code is located and execute the following command
If you still encounter problems running the code in VSCode, try running the Go code directly in the system terminal. Navigate to the directory where the code is located and execute what command?
To run Go code in the system terminal, follow these steps:
- Open the system terminal.
- Navigate to the directory where the code is located, use the
cd
command.
For example, if your code is in a folder called “my_go_project”, you can type:
cd my_go_project
- Next, run the Go code with the
go run
command. Be sure to specify the name of the file that primarily contains themain
function. For example, if the main file is namedmain.go
, enter the following command:
go run main.go
This will compile and run the Go code in that file.
In your opinion, for go language development, use vscode or goland?
Choosing to use VSCode or GoLand for Go language development mainly depends on personal preferences and needs. Here are some of their characteristics:
VSCode:
- Free and open source.
- Rich plug-ins support multiple programming languages and functions.
- Lightweight and takes up less system resources.
- Install the Go extension to get code hinting, error checking, debugging, and more for Go.
GoLand:
- A professional Go language integrated development environment (IDE) developed by JetBrains.
- Smarter and more powerful code completion and navigation.
- Supports advanced features such as refactoring, code analysis, built-in test runner, and more.
- Provide more powerful debugging tools.
- Paid software with a free trial version.
In conclusion, if you want a lightweight and free editor, you can choose VSCode. If you need a more powerful dedicated Go language IDE and are willing to pay for it, GoLand is a great option. Ultimately, it is recommended to try out both editors and make a decision based on your own preferences and needs.
How to configure the go language development environment in ubuntu?
It seems that there are no more times.
Configuring the Go language development environment in Ubuntu is divided into the following steps:
- Download and install the Go compiler
- set environment variables
- verify installation
- Install VSCode (optional, recommended IDE)
1. Download and install the Go compiler
First, visit the official Go download page to get the latest version of Go. Choose the version appropriate for your system and copy the link to your clipboard.
Use the wget
command to download the Go compressed package:
$ wget https://dl.google.com/go/goVERSION.linux-amd64.tar.gz
Please replace VERSION
with the Go version number you want to download, for example 1.17.1
.
Unzip the downloaded file to the /usr/local
directory:
$ sudo tar -C /usr/local -xzf goVERSION.linux-amd64.tar.gz
2. Set environment variables
Set the GOROOT
and GOPATH
environment variables to point to the path where the Go compiler is located and the Go workspace. Also, add the Go binaries to your system’s PATH
.
Open the ~/.profile
or ~/.bashrc
file:
$ nano ~/.profile
Add the following at the end of the file:
export GOROOT=/usr/local/go export GOPATH=$HOME/go export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
Save and exit the editor. Then, run the following command for the changes to take effect:
$ source ~/.profile
3. Verify installation
To verify that you have set the Go locale environment variables correctly in your Ubuntu system, follow these steps:
-
Open a new terminal window.
-
Verify the
GOROOT
andGOPATH
environment variables. Type the following command and press Enter:echo $GOROOT echo $GOPATH
If the corresponding path information is returned, then the environment variable is set correctly.
-
Check if the
go
command is available. Type the following command and press Enter:go version
If the version number of Go is displayed (such as
go1.17.2 linux/amd64
), it means that Go has been installed correctly and can be used in your system. -
Write a simple Go program to test the environment. Create a file named
hello.go
in any directory and enter the following code:package main import "fmt" func main() {<!-- --> fmt.Println("Hello, Go!") }
-
Run the program with the
go run
command:go run hello.go
If “Hello, Go!” is output, it means that your Go environment configuration is successful.