2015-11-20 13:55
浏览 1.4k

如何在Visual Studio Code中保存时运行`go fmt`?

How to make Visual Studio Code (or Go Programming Language Extension) to run go fmt (or other tools/commands) on save? Even auto save?

Update: It is working now perfectly inside VSCode, at this time; just need to add some config files inside .vscode directory (I use these).

Update 2019: This question is old. The VSCode Go extension, has all you need to develop in Go, now.

图片转代码服务由CSDN问答提供 功能建议

如何使Visual Studio Code(或Go编程语言扩展)运行 go fmt (或其他工具/命令)是否保存? 甚至自动保存?

更新: 目前,它在VSCode中可以正常运行; 只需在 .vscode 目录内添加一些配置文件(我使用这些)。

更新2019: 这个问题很旧。 VSCode Go扩展,现在拥有您需要在Go中开发的所有功能。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dqeznd1697 2015-11-20 14:15

    Its not possible at the moment but its being worked on https://github.com/Microsoft/vscode-go/issues/14

    打赏 评论
  • douluohan3403 2015-11-21 13:18

    I'm not familiar with 'go fmt' specifically, but you can create a simple vscode extension to handle on save event and execute any arbitrary command passing the file path as an argument.

    Here's a sample that just calls echo $filepath:

    import * as vscode from 'vscode';
    import {exec} from 'child_process';
    export function activate(context: vscode.ExtensionContext) {
        vscode.window.showInformationMessage('Run command on save enabled.');
        var cmd = vscode.commands.registerCommand('extension.executeOnSave', () => {
            var onSave = vscode.workspace.onDidSaveTextDocument((e: vscode.TextDocument) => {
                // execute some child process on save
                var child = exec('echo ' + e.fileName);
                child.stdout.on('data', (data) => {

    And the package file:

        "name": "Custom onSave",
        "description": "Execute commands on save.",
        "version": "0.0.1",
        "publisher": "Emeraldwalk",
        "engines": {
            "vscode": "^0.10.1"
        "categories": [
        "activationEvents": [
        "main": "./out/src/extension",
        "contributes": {
            "commands": [{
                "command": "extension.executeOnSave",
                "title": "Execute on Save"
        "scripts": {
            "vscode:prepublish": "node ./node_modules/vscode/bin/compile",
            "compile": "node ./node_modules/vscode/bin/compile -watch -p ./"
        "devDependencies": {
            "typescript": "^1.6.2",
            "vscode": "0.10.x"

    The extension is enabled via cmd+shift+p then typing "Execute on Save", but it could be reconfigured to start via another command including "*" which would cause it to load any time VSCode loads.

    Once the extension is enabled, the event handler will fire whenever a file is saved (NOTE: this doesn't appear to work when file is first created or on Save as...)

    This is just a minor modification of a yo code scaffolded extension as outlined here: https://code.visualstudio.com/docs/extensions/example-hello-world


    Here's a Visual Studio Code extension I wrote for running commands on file save. https://marketplace.visualstudio.com/items/emeraldwalk.RunOnSave.

    打赏 评论
  • dsgk40568 2018-11-28 07:02

    Now, the feature has been implemented, you can enable format on save:

    1. Open Settings (Ctrl + ,)
    2. Search for editor.formatOnSave and set to true

    You Go code will be formatted automatically on Ctrl + s

    打赏 评论

相关推荐 更多相似问题