分享
  1. 首页
  2. 文章

VScode Golang 编译任务Task.json

罐头过期 · · 3242 次点击 · · 开始浏览
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

用过不少Golang Ide 工具最后感觉还是VSCode好用,只是VSCode针对Golang Build 和 Install 任务执行没有LiteIDE方便,特地整理了Task.json任务并且绑定好了快捷键,现在给大家分享下。

项目整体结构如下


目录结构.png

task.json

{
 // See https://go.microsoft.com/fwlink/?LinkId=733558
 // for the documentation about the tasks.json format
 "version": "2.0.0",
 "tasks": [
 {
 "type": "shell",
 "label": "Make Lib",
 "command":"go", 
 "options": {
 "cwd": "${workspaceRoot}\\src",
 "env": {
 "GOPATH": "E:\\tygo;C:\\Users\\Administrator\\go"
 }
 }, 
 "args": [
 "install", //build
 "-x",
 "ty",
 "tx",
 ], 
 },
 {
 "type": "shell",
 "label": "Gate Build",
 "command":"go",
 "options": {
 "cwd": "${workspaceRoot}\\src\\gate",
 "env": {
 "GOPATH": "E:\\tygo;C:\\Users\\Administrator\\go"
 }
 }, 
 "args": [
 "build",
 "-o",
 "${workspaceRoot}\\bin\\gate.exe",
 ],
 },
 {
 "type": "shell",
 "label": "Main Build",
 "command":"go",
 "options": {
 "cwd": "${workspaceRoot}\\src",
 "env": {
 "GOPATH": "E:\\tygo;C:\\Users\\Administrator\\go"
 }
 }, 
 "args": [
 "build",
 "-o",
 "${workspaceRoot}\\bin\\main.exe",
 ],
 }, 
 {
 "label": "Build",
 "dependsOn": ["Gate Build", "Main Build"],
 // "group": {
 // // 从这里
 // "kind": "build",
 // "isDefault": true
 // }, // 到这里,设置为默认构建任务,按Ctrl+Shift+B立即执行,不必选择 
 "presentation": {
 //shell命令输出的内容并不弹出来提醒
 // "reveal": "silent"
 }, 
 "problemMatcher": [] 
 } 
 ]
}

绑定好快捷键Ctrl+F7 编译项目包含的gate.exe 和main.exe 两个可执行文件到bin目录,Ctrl+F8 编译动态库到pkg目录。
keybingings.json

// 将键绑定放在此文件中以覆盖默认值
[{
 "key": "CTRL+F7",
 "command": "workbench.action.tasks.runTask",
 "args": "Build",
 "when": "editorTextFocus"
},
{
 "key": "CTRL+F8",
 "command": "workbench.action.tasks.runTask",
 "args": "Make Lib",
 "when": "editorTextFocus"
}]

注意:如果项目包含自己编写的库文件,需要在gopath 在制定对应的目录,编译才能成功。
settings.json

{
 "go.buildOnSave": "package",
 "go.buildFlags": ["-o","E:\\tygo"],
 // "go.lintOnSave": true,
 // "go.vetOnSave": true,
 "go.buildTags": "",
 "go.lintFlags": [],
 "go.vetFlags": [],
 "go.coverOnSave": false,
 "go.useCodeSnippetsOnFunctionSuggest": false,
 // "go.formatOnSave": true,
 "go.lintTool": "golint", 
 "go.formatTool": "goreturns",
 "go.goroot": "C:\\Go",
 "go.gopath": "E:\\tygo;C:\\Users\\Administrator\\go",
 "go.gocodeAutoBuild": true,
 "go.inferGopath": true,
}

第一次写没什么经验,写的不好欢迎大家指正,感谢大家!


有疑问加站长微信联系(非本文作者)

本文来自:简书

感谢作者:罐头过期

查看原文:VScode Golang 编译任务Task.json

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

关注微信
3242 次点击
暂无回复
添加一条新回复 (您需要 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传

用户登录

没有账号?注册
(追記) (追記ここまで)

今日阅读排行

    加载中
(追記) (追記ここまで)

一周阅读排行

    加载中

关注我

  • 扫码关注领全套学习资料 关注微信公众号
  • 加入 QQ 群:
    • 192706294(已满)
    • 731990104(已满)
    • 798786647(已满)
    • 729884609(已满)
    • 977810755(已满)
    • 815126783(已满)
    • 812540095(已满)
    • 1006366459(已满)
    • 692541889

  • 关注微信公众号
  • 加入微信群:liuxiaoyan-s,备注入群
  • 也欢迎加入知识星球 Go粉丝们(免费)

给该专栏投稿 写篇新文章

每篇文章有总共有 5 次投稿机会

收入到我管理的专栏 新建专栏