Ubuntu Go language environment configuration [GPT-4 version]

Go language training online course

old boys

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

Insert image description here

【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:

  1. 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.

  2. Configure the GOPATH environment variable

    Set GOPATHthe environment variable to the directory where you wish to create the Go workspace. For example, set it to ~/go(on Unix systems) or C:\Users\YourName\go(on Windows systems).

  3. Install VSCode

    Visit https://code.visualstudio.com/, select the appropriate installation file according to your operating system and install it.

  4. Install Go plugin

    Open VSCode, click the extension button (rectangular icon) in the sidebar, search Go, find Go Team at Googlethe plug-in provided by and click to install.

  5. Configure Go plugin

    After the installation is complete, click in the lower right corner Reloadto reload VSCode. Then press Ctrl+Shift+P(on Mac Cmd+Shift+P) Enter Go: Install/Update Tools, check all tools and click OK.

  6. 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
  7. 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_foldercreate a new Go file in (for example: main.go).
  8. Writing Go code

    In main.gothe file, write your Go code. For example:

    package main
    
    import "fmt"
    
    func main() {
          
          
        fmt.Println("Hello, world!")
    }
    
  9. 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:

  1. Install the Go extension First, search for and install the Go
    extension in the VSCode extension store .

  2. Install Go language tools

    Open the command palette via Ctrl+Shift+Por Cmd+Shift+P(Mac) and enter Go: Install/Update Toolsand execute. This will automatically install Go language related tools for you. Please make sure you have at least the following tools installed: dlv, gopls, gocodeand go-outline.

  3. Create and configure launch.json file

    Under the project root directory, create a .vscodesubfolder called (if necessary) and create a new launch.jsonfile 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.
  4. 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:

  1. 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.

  2. 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
  1. 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
  1. 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
  1. Restart Visual Studio Code
    Close and restart Visual Studio Code. Make sure you have installed the Go tools correctly.

  2. 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:

  1. Open Visual Studio Code.
  2. Click the 查看( View) tab in the upper left corner.
  3. 输出Select ( Output) from the drop-down menu , which will open the output window in the bottom panel (Terminal area).
  4. 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:

  1. 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
    
  2. Install and configure the Go plugin:

    • Open Visual Studio Code.
    • Use the keyboard shortcut Ctrl+Shift+Xor 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.
  3. Update Go plug-ins and dependent tools:

    • Open the command panel of VSCode and use the shortcut key Ctrl+Shift+Por Cmd+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.
  4. Check the code and modules:

    • Make sure your Go code is correct and has no syntax errors.
    • Check go.modthat the file exists and is configured correctly.
  5. 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:

  1. Open the system terminal.
  2. Navigate to the directory where the code is located and use cdthe command.

For example, if your code is in a folder called "my_go_project" you would enter:

cd my_go_project
  1. Next, use go runthe command to run the Go code. Be sure to specify maina file name that contains primarily functions. For example, if the main file is named main.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?

Insert image description here
There seems to be no more times.

Configuring the Go language development environment in Ubuntu is divided into the following steps:

  1. Download and install the Go compiler
  2. Set environment variables
  3. Verify installation
  4. 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 wgetthe command to download the Go compressed package:

$ wget https://dl.google.com/go/goVERSION.linux-amd64.tar.gz

Please VERSIONreplace with the Go version number you want to download, for example 1.17.1.

Unzip the downloaded file to /usr/localthe directory:

$ sudo tar -C /usr/local -xzf goVERSION.linux-amd64.tar.gz

2. Set environment variables

Set GOROOTand GOPATHenvironment 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 ~/.profileor ~/.bashrcfile:

$ 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:

  1. Open a new terminal window.

  2. Validation GOROOTand GOPATHenvironment 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.

  3. Check goif 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.

  4. Write a simple Go program to test the environment. Create a file named in any directory hello.goand enter the following code:

    package main
    
    import "fmt"
    
    func main() {
          
          
        fmt.Println("Hello, Go!")
    }
    
  5. go runRun this program using the command:

    go run hello.go
    

    If "Hello, Go!" is output, your Go environment is configured successfully.

Guess you like

Origin blog.csdn.net/CSDN_YJX/article/details/130927408