教程集 www.jiaochengji.com
教程集 >  Golang编程  >  golang教程  >  正文 VSCode搭建Go语言开发环境

VSCode搭建Go语言开发环境

发布时间:2023-03-18   编辑:jiaochengji.com
教程集为您提供VSCode搭建Go语言开发环境等资源,欢迎您收藏本站,我们将为您提供最新的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

https://blog.csdn.net/ZHAOJUNWEI08/article/details/83578310

VSCode

https://blog.csdn.net/ZHAOJUNWEI08/article/details/83578184

修改vscode插件位置

https://blog.csdn.net/ZHAOJUNWEI08/article/details/86021665

安装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就可以启动调试,也可以从左侧的面板进入。

 

到此这篇关于“VSCode搭建Go语言开发环境”的文章就介绍到这了,更多文章或继续浏览下面的相关文章,希望大家以后多多支持JQ教程网!

您可能感兴趣的文章:
VSCode搭建Go语言开发环境
Go 语言 win 环境搭建及开发工具配置
Go语言环境搭建(Windows版本)2020
GO语言 VSCode开发环境搭建 [Mac]
Windows10下VScode环境编译运行go语言
五款最棒的Go语言开发工具?
ubuntu 16.04安装go语言并配置VSCode使用环境
windows环境下,VS Code配置GO语言开发环境
【保姆级教程】手把手教你进行Go语言环境安装及相关VSCode配置
Go语言vscode环境配置

[关闭]
~ ~