如何使用VSCode中的Code Runner插件执行golang代码

栏目: Go · 发布时间: 5年前

内容简介:如何使用VSCode中的Code Runner 插件执行golang代码project的文档结如下mylab

如何使用VSCode中的Code Runner 插件执行golang代码

project的文档结如下

mylab

|—- main.go

|—- test.go

main.go

package main

func main() {
	test()
}

test.go

package main

func test() {
	println("in test")
}

Code Runner插件的Doc地址

找到如下Configuration的配置说明

Configuration  Make sure the executor PATH of each language is set in the environment variable. You could also add entry into code-runner.executorMap to set the executor PATH. e.g. To set the executor PATH for ruby, php and html:
{
    "code-runner.executorMap": {
        "javascript": "node",
        "php": "C:\\php\\php.exe",
        "python": "python",
        "perl": "perl",
        "ruby": "C:\\Ruby23-x64\\bin\\ruby.exe",
        "go": "go run",
        "html": "\"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe\"",
        "java": "cd $dir && javac $fileName && java $fileNameWithoutExt",
        "c": "cd $dir && gcc $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt"
    }
}

Supported customized parameters

  • $workspaceRoot: The path of the folder opened in VS Code
  • $dir: The directory of the code file being run
  • $dirWithoutTrailingSlash: The directory of the code file being run without a trailing slash
  • $fullFileName: The full name of the code file being run
  • $fileName: The base name of the code file being run, that is the file without the directory
  • $fileNameWithoutExt: The base name of the code file being run without its extension
  • $driveLetter: The drive letter of the code file being run (Windows only)
  • $pythonPath: The path of Python interpreter (set by Python: Select Interpreter command)

Please take care of the back slash and the space in file path of the executor

Back slash: please use

If there ares spaces in file path, please use \” to surround your file path

You could set the executor per filename glob:

{
    "code-runner.executorMapByGlob": {
        "pom.xml": "cd $dir && mvn clean package",
        "*.test.js": "tap",
        "*.js": "node"
    }
}

可以看到 go 的执行命令默认配置成了go run, 并且默认执行当前打开的文件。 所以需要利用$dir这个变量run整个main包所涉及到的所有文件。并且可以正则匹配文件名。

查看一下go run这个命令的参数解释,由于众所周知的原因,我们只能上国内的 替代官网

Compile and run Go program Usage:

go run [build flags] [-exec xprog] package [arguments…] Run compiles and runs the named main Go package. Typically the package is specified as a list of .go source files, but it may also be an import path, file system path, or pattern matching a single known package, as in ‘go run .’ or ‘go run my/cmd’.

By default, ‘go run’ runs the compiled binary directly: ‘a.out arguments…’. If the -exec flag is given, ‘go run’ invokes the binary using xprog:

‘xprog a.out arguments…’. If the -exec flag is not given, GOOS or GOARCH is different from the system default, and a program named go $GOOS $GOARCH_exec can be found on the current search path, ‘go run’ invokes the binary using that program, for example ‘go_nacl_386_exec a.out arguments…‘. This allows execution of cross-compiled programs when a simulator or other execution method is available.

The exit status of Run is not the exit status of the compiled binary.

For more about build flags, see ‘go help build’. For more about specifying packages, see ‘go help packages’.

找到了,使用 go run . 来跑整个package的。

在VSCode的.vscode目录下创建settings.json文件

自定义这个project的用户配置如下

{
    "code-runner.executorMap": {
        "go": "cd $dir && go run .",
    },
    "code-runner.executorMapByGlob": {
        "$dir\\*.go": "go"
    }
}

点击Cdoe Runner在VSCode GUI界面提供的RUN按钮

[Running] cd "d:\Users\polar\go\src\mylab\" && go run .
in test

[Done] exited with code=0 in 3.564 seconds

OK!

当然也可以用VSCode Debug 的launch.json来配置,但是那个debug模式。并不是go run。


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

测出转化率:营销优化的科学与艺术

测出转化率:营销优化的科学与艺术

【美】高尔德(Goward,C.) / 谭磊、唐捷译 / 电子工业出版社 / 2014-10-1 / 68.00元

本书作者通过已成功实现大幅提升转化率的案例,展示了大量以营销为核心的电子商务网站的测试设计方法及转化优化方案。书中作者强调了测试及优化思维的重要性,并就实现方法做了详细讲解。 通过本书,读者将学到如何能够在网站遇到发展和收入瓶颈时,测试出存在的问题并找到解决方案;如何可以深入地了解客户需求,并以此为基础优化网站,使其达到提升转化率的目的;如何提升网站的竞争优势,把在线营销渠道变成高效的转化通......一起来看看 《测出转化率:营销优化的科学与艺术》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

html转js在线工具
html转js在线工具

html转js在线工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具