go - Why does vscode delete golang source on save

ID : 131338

viewed : 7

Tags : govisual-studio-codevscode-settingsgo

Top 5 Answer for go - Why does vscode delete golang source on save

vote vote

90

config both editor.formatOnSave and editor.codeActionsOnSave in the settings.json:

"[go]": {          "editor.formatOnSave": false,         "editor.codeActionsOnSave": {             "source.organizeImports": false         },     },     "go.formatTool": "gofmt", 
vote vote

82

Basically, your formatOnSave is on for go which is causing this problem.

To disable it, go to your Command Palette (Ctrl+Shift+P), type in "configure language specific settings", and look for Go.

You should now see a json file where you can add the following setting to it:

"editor.formatOnSave": false. 

This is how the json file looks like if you just have on setting modified for go:

{     "window.zoomLevel": 1,     "[go]": {         "editor.formatOnSave": false,     } } 
vote vote

75

By default Format Tool is set to "goreturns" in settings.json, change it to "fmt":

{     "go.formatTool": "gofmt" } 

Now you can comment the imports:

import (     "fmt"     // "reflect"     // "math/rand" ) 
vote vote

69

I had a similar issue that was caused by not having the right case on method names.

In the following code import "fmt" would disappear.

package main  import "fmt"  func main() {   fmt.println("hello world") } 

Solution I should be calling Println NOT println ! Note the uppercase P. Once changed goreturns adds the import instead of removing it.

vote vote

54

Ctrl+Shift+P --> Configure Language Specific Settings

       "editor.insertSpaces": false,         "editor.formatOnSave": **false,**         "editor.codeActionsOnSave": {             "source.organizeImports": **false**         }     } } 

Top 3 video Explaining go - Why does vscode delete golang source on save

Related QUESTION?