VSCode搭建Go语言开发环境

目录

1、基本环境配置

golang

VSCode

修改vscode插件位置

安装golang的插件

2、测试工程

新建工程

环境变量

用VSCode打开hello.go文件,或者在VSCode中打开hello文件夹。

3、vscode运行配置

launch.json,

task.json,方式一:写cmd文件,由task进行调用

task.json,方式二:task中直接调用go命令

settings.json,系统的设置,开发环境的一些设置参数。

4、调试



1、基本环境配置

golang

Golang工具安装_安装gometalinter-CSDN博客

VSCode

VSCode工具安装_vscode 和 vscode insider-CSDN博客

修改vscode插件位置

Windows 链接彻底解决 vscode插件安装位置问题_c:\users\junch\.vscode-insiders\extensions\ms-vsco-CSDN博客

安装golang的插件

2、测试工程

新建工程

新建个文件夹,命名hello,G:\WorkSpace\VSCode\go\hello

写一个hello.go的文件放在目录下,G:\WorkSpace\VSCode\Python\go\hello.go

环境变量

如果你Golang安装的比较好的话,这里其实不需要去设置,可以在cmd中用go env命令查看


用VSCode打开hello.go文件,或者在VSCode中打开hello文件夹。

打开go文件的时候,右下角会提示要安装一些工具,如图:

这些是安装Golang的时候安装好的,这里如果提示,应该是GOBIN的问题。

你可以手动将文件下载到对应的目录,然后go install去安装。

很多都安装失败了,我的bin目录是E:\GoLang\bin  (${GOBIN})

所以手动在github.com上面下载响应的包,放到E:\GoLang\src\github.com中。(${GOROOT}\src\github.com)

下载的时候拷贝github.com/mdempsky/gocode 到浏览器,enter即可跳转

手动安装go install 后面是路径,不需要写全,如果${GOROOT}\src\github.com下没有,会到${GOPATH}\src\github.com下去找.

3、vscode运行配置

launch.json,

调试->打开配置,也可以用添加配置(如果VSCODE配置多个开发环境的话):

这里一般不用配置,安装完插件后就是OK的。

task.json,方式一:写cmd文件,由task进行调用

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build project",
            "type": "shell",
            "command": "${workspaceRoot}\\.vscode\\build_project.cmd",
            "args": [],
            "presentation": {
                // Reveal the output only if unrecognized errors occur.
                "reveal": "always"
            },
            // Use the standard MS compiler pattern to detect errors, warnings and infos
            "problemMatcher": {
                "source": "compile",
                "fileLocation": [
                    "relative",
                    "${workspaceRoot}"
                ],
                "pattern": {
                    "regexp": "(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
                    "file": 1,
                    "line": 2,
                    "column": 3,
                    "severity": 4,
                    "message": 5
                }
            }
        }
    ]
}

cmd文件

echo off

REM *****************************************************************
REM
REM Build demo 
REM Precondition: You must specify following enviroment variables exeternal: 
REM %SSH_HOST%
REM
REM ****************************************************************
echo "enter %0 ..."
cd %~dp0

REM 设置参数,系统临时变量

echo "++++gopath=%GOPATH%"
echo "++++goarch=%GOARCH%"

SET FILE_LIST="../main.go"

echo on
go build %FILE_LIST%

echo "exit %0 ..."

echo on

task.json,方式二:task中直接调用go命令

command直接写成go build ${workspaceRoot}\\main.go,这个是在文件少的情况。

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build project",
            "type": "shell",
            "command": "go",
            "args": [
                "build",
                "${workspaceRoot}\\main.go"
            ],
            "presentation": {
                // Reveal the output only if unrecognized errors occur.
                "reveal": "always"
            },
            // Use the standard MS compiler pattern to detect errors, warnings and infos
            "problemMatcher": {
                "source": "compile",
                "fileLocation": [
                    "relative",
                    "${workspaceRoot}"
                ],
                "pattern": {
                    "regexp": "(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
                    "file": 1,
                    "line": 2,
                    "column": 3,
                    "severity": 4,
                    "message": 5
                }
            }
        }
    ]
}

settings.json,系统的设置,开发环境的一些设置参数。

 文件->首选项->设置  现在设置有界面了,可以选择在json中编辑。

go语言的不需要特殊配置,我这里也没有配置

当然你也可以指定gopath (注意:不管指定那个gopath,必须要保证gobin的正确,要么就是在环境变量中定义一个GOBIN,要么在你自己指定的gopath中需要有bin和src目录,并且要有相应的东西)

goroot就是go的安装目录,这个不需要去改。


4、调试

按F5就可以启动调试,也可以从左侧的面板进入。

 

### 配置 VSCodeGo 语言开发环境 #### 安装 Go 编程语言 在配置 VSCode 前,需先完成 Go 语言的基础安装。这通常涉及以下几个方面: - **下载并安装 Go** 访问官方站点下载适合操作系统的版本,并按照说明进行安装[^1]。完成后可通过命令 `go version` 来验证安装是否成功。 - **设置环境变量** 确保已正确设置了 `$GOPATH` 和 `$GOROOT` 环境变量。这些路径用于指定工作目录以及 Go 源代码的位置[^2]。 #### 安装 Visual Studio Code (VSCode) 如果尚未安装 VSCode,则可以从其官网获取最新版并完成安装过程。此工具支持多平台运行,在 Windows、macOS 或 Linux 上均可使用。 #### 插件安装与配置 为了提升开发体验,建议安装以下扩展: ```plaintext Go 扩展(由 Microsoft 提供) ``` 该插件集成了调试器、格式化程序以及其他实用功能来增强编码效率[^3]。 执行如下步骤加载必要组件: 1. 启动 VSCode; 2. 转至左侧活动栏中的“扩展”图标; 3. 输入关键词 “Go”,找到对应项点击安装按钮即可。 #### 测试项目创建 建立一个新的文件夹作为项目的根目录,接着打开终端窗口输入下面这条指令初始化模块结构: ```bash go mod init your_project_name ``` 随后可以尝试编写简单的测试脚本以确认一切正常运作。例如新建名为 main.go 文件写入以下内容: ```go package main import "fmt" func main() { fmt.Println("Hello, world!") } ``` 最后保存更改回到控制台键入命令启动应用程序查看输出结果: ```bash go run main.go ``` 以上即完成了整个流程概述,具体细节可能依据个人需求有所调整。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值