Table of contents
- Go language training online course
- Install go
- The process given by GPT-4
- How to correctly configure Go debugging related settings in VSCode?
- What 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 Go language code?
- If you still have problems running the code within VSCode, try running the Go code directly in the system terminal. Navigate to the directory where the code is located and execute what command?
- Do you think, for Go language development, should you use vscode or goland?
- How to configure the go language development environment in ubuntu?
Go language training online course
Prompt the learning progress of the project: https://learningprompt.wiki/docs/chatGPT/tutorial-basics/%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8%E5%9C %BA%E6%99%AF%20&%20%E4%BD%BF%E7%94%A8%E6%8A%80%E5%B7%A7/scenario-1-ask-questions
Install go
Official website: https://golang.google.cn/dl/
Use administrator identity to unzip go to /usr/local
sudo tar -C /usr/local -xzf go1.15.2.linux-amd64.tar.gz
Check the go version
go version
【Ubuntu】Ubuntu set and view environment variables
//我的配置
root@long-WUJIE-14:/home/long# sudo vim ~/.bashrc # 推荐
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
公共的 文档 config.yaml Miniconda3-latest-Linux-x86_64.sh
模板 下载 Documents nohup.out
视频 音乐 go note
图片 桌面 GO-project PycharmProjects
未命名.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 language environment are as follows:
-
Install 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
GOPATH
the environment variable to the directory where you wish to create the 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 Go plugin
Open VSCode, click the extension button (rectangular icon) in the sidebar, search
Go
, findGo Team at Google
the plug-in provided by and click to install. -
Configure Go plugin
After the installation is complete, click in the lower right corner
Reload
to reload VSCode. Then pressCtrl+Shift+P
(on MacCmd+Shift+P
) 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
- Go into this 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 in the sidebar of VSCode, right-click on the new file icon, and then choose to
your_project_folder
create a new Go file in (for example:main.go
).
- Open the folder you just created:
-
Writing Go code
In
main.go
the 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 debugging settings in Visual Studio Code (VSCode), follow these steps:
-
Install the Go extension First, search for and install the Go
extension in the VSCode extension store . -
Install Go language tools
Open the command palette via
Ctrl+Shift+P
orCmd+Shift+P
(Mac) and enterGo: Install/Update Tools
and execute. This will automatically install Go language related tools for you. Please make sure you have at least the following tools installed:dlv
,gopls
,gocode
andgo-outline
. -
Create and configure launch.json file
Under the project root directory, create a
.vscode
subfolder called (if necessary) and create a newlaunch.json
file within it. Then, add the following content 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, which should be "go" here."request"
: Specify the debugging type, here it is "launch". If you want to attach to an existing process, select "attach"."mode"
: Debug mode, can be set to "auto", "debug", "test" or "remote"."program"
: The path of the Go program to be debugged.${workspaceFolder}
Represents 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 network connection is working properly. Some countries/regions may cause installation to fail due to network restrictions. You can use a proxy such as a VPN to solve this problem. -
Setting up 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 Go tools installed. Run the following command in the terminal to update the Go tools:
go get -u all
- Manually install the extension package.
If automatic installation fails in VSCode, you can try to manually install the required extension package. First, enter the following command in the terminal to obtain 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 you have installed the Go tools correctly. -
Check the logs
If the above method still doesn't work...
I have restarted Visual Studio Code, what should I do next to check the logs?
To check Visual Studio Code’s logs, follow these steps:
- Open Visual Studio Code.
- Click the
查看
(View
) tab in the upper left corner. 输出
Select (Output
) from the drop-down menu , which 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 (defaults to "Tasks" or "Tasks") and select
Log (Window)
or日志(窗口)
to view VSCode-related logs.
You should now be able to view the Visual Studio Code logs. Here you can find detailed logging of errors, warnings, or other information. If you need to query the logs of a specific plug-in, please select the corresponding plug-in in the drop-down menu of the output window.
What should I do if I still cannot successfully run Go language code?
If you have trouble running Go language code, try the following:
-
Check the environment configuration: Make sure the Go language is installed correctly and added 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 in the left column to open the Extensions panel. - Type "Go" in the search box and find the plugin named "Go" by "go-team at Google" and click the Install button.
- After the installation is complete, restart Visual Studio Code.
-
Update Go plug-ins and dependent tools:
- Open the command panel of VSCode and use the shortcut key
Ctrl+Shift+P
orCmd+Shift+P
(macOS). - Type "Go: Install/Update Tools" and press Enter to execute this command.
- In the pop-up window, select all listed tools and click the "OK" or "Confirm" button to start installing or updating the tools.
- Open the command panel of VSCode and use the shortcut key
-
Check the code and modules:
- Make sure your Go code is correct and has no syntax errors.
- Check
go.mod
that the file exists and is configured correctly.
-
Run the code using the terminal:
If you still have problems running the code within 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 have problems running the code within 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 and use
cd
the command.
For example, if your code is in a folder called "my_go_project" you would enter:
cd my_go_project
- Next, use
go run
the command to run the Go code. Be sure to specifymain
a file name that contains primarily functions. 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 this file.
Do you think, for Go language development, should you 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.
- Plug-ins are rich and support multiple programming languages and functions.
- Lightweight and takes up less system resources.
- Install the Go extension to get code prompts, error checking, debugging and other functions 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, providing 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, then GoLand is a good choice. Ultimately, it is recommended to try both editors and make a decision based on your preferences and needs.
How to configure the go language development environment in ubuntu?
There seems to be 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 wget
the command to download the Go compressed package:
$ wget https://dl.google.com/go/goVERSION.linux-amd64.tar.gz
Please VERSION
replace with the Go version number you want to download, for example 1.17.1
.
Unzip the downloaded file to /usr/local
the directory:
$ sudo tar -C /usr/local -xzf goVERSION.linux-amd64.tar.gz
2. Set environment variables
Set GOROOT
and GOPATH
environment variables to point to the path where the Go compiler is located and the Go workspace. At the same time, add the Go binaries to your system PATH
.
Open ~/.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 the Go language environment variables set correctly on your Ubuntu system, follow these steps:
-
Open a new terminal window.
-
Validation
GOROOT
andGOPATH
environment variables. Enter the following command and press Enter:echo $GOROOT echo $GOPATH
If the corresponding path information is returned, it means that the environment variables are set correctly.
-
Check
go
if the command is available. Enter the following command and press Enter:go version
If the Go version number (for example
go1.17.2 linux/amd64
) is displayed, Go has been installed correctly and can be used on your system. -
Write a simple Go program to test the environment. Create a file named in any directory
hello.go
and enter the following code:package main import "fmt" func main() { fmt.Println("Hello, Go!") }
-
go run
Run this program using the command:go run hello.go
If "Hello, Go!" is output, your Go environment is configured successfully.