vscode、golang开发环境、win10本地调试、ubuntu本地调试

本篇记录 golang在win10的vscode环境中,开发调试环境搭建过程记录,谨防忘记。

Win10 环境搭建记录

环境64位的win10 操作系统。

1.安装软件

安装软件顺序和内容如下:

Git:https://gitforwindows.org/
Go:https://golang.google.cn/dl/
vsocde:https://code.visualstudio.com/

全部采用确认路径安装,无特殊说明。

PS C:\Users\ljb> git version
git version 2.32.0.windows.2
PS C:\Users\ljb> go version 
go version go1.16.7 windows/amd64
//vscod版本
version:1.59 

2.编写测试程序

2.1 打开 workspace 文件夹

选择本地工作路径。vscode的workspace路径选择

2.2 在此workspace下新建main.go测试程序

内容如下:

package main
import (
	"fmt"
)
func main(){
    
    
fmt.Printf("hello world\n")
}

保存文件后,vscode会提示安装插件文件;如果此时安装会提示错误,因为网络被墙的原因。

3. 设置golang的代理服务器

3.1

在vscode中、打开终端、选择new terminal,在终端命令行中输入如下内容

go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.io,direct

第一条、开启go module模块功能;
第二条、设置代理服务器地址;
重新打开vscode后,提示安装插件,再次install all 安装所以的插件,就能够成功。

3.2 配置golang module

打开vscode的terminal窗口,此时缺省路径应该就是刚刚用户选择的workspace 路径
执行如下命令:

go mod init example.com/v
go mod tidy

配置 golang的module 包管理模式,会生成go.mod文件。

  • 可以采用ls命令查看文件;
PS D:\golang\src\workspace\localDebug> ls
    目录: D:\golang\src\workspace\localDebug
Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----        2021/8/15     17:59                .vscode
-a----        2021/8/15     18:07             27 go.mod
-a----        2021/8/15     21:33             87 httpClient.go
  • 关于 go mod 命令使用方法,可以采用 go mod help 读者自行查看。
PS D:\golang\src\workspace\localDebug> go mod help
Go mod provides access to operations on modules.

Note that support for modules is built into all the go commands,       
not just 'go mod'. For example, day-to-day adding, removing, upgrading,
and downgrading of dependencies should be done using 'go get'.
See 'go help modules' for an overview of module functionality.
Usage:
        go mod <command> [arguments]
The commands are:
        download    download modules to local cache
        edit        edit go.mod from tools or scripts
        graph       print module requirement graph
        init        initialize new module in current directory
        tidy        add missing and remove unused modules
        vendor      make vendored copy of dependencies
        verify      verify dependencies have expected content
        why         explain why packages or modules are needed

Use "go help mod <command>" for more information about a command.

PS D:\golang\src\workspace\localDebug>

4. debug 代码

选择 run -> start debug 菜单时,会提示新建 launch.json 调试配置文件,选择 “Launch Package” 项后,vscode会自动生成如下样式的文件,内容如下:

{
    
    
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
    
    
            "name": "Launch Package",
            "type": "go",
            "request": "launch",
            "mode": "auto",
            "program": "${fileDirname}"
        }
    ]
}

此文件时json格式的配置文件,此内容是本地代码调试配置。

5. debug result

如果读者配置顺利的话,在debug console 窗体中、程序运行输出内容如下:

在这里插入图片描述至此本地调试配置完成。

ubuntu 16 环境搭建过程记录

安装软件内容:

  1. git
  2. golang
  3. vscode
    安装方法请读者自选。
  • 配置方法与win10过程相同,配置过程 2~5 步骤相同。

猜你喜欢

转载自blog.csdn.net/weixin_38387929/article/details/119720064