2015-11-20 13:55

如何在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.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • dqeznd1697 dqeznd1697 6年前

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

    点赞 评论 复制链接分享
  • dsgk40568 dsgk40568 3年前

    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

    点赞 评论 复制链接分享
  • douluohan3403 douluohan3403 6年前

    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.

    点赞 评论 复制链接分享