Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does vscode delete golang source on save

why can't save these line of code in vscode with golang extension

package app  import (   "fmt" )  //this is just func func TestingHello(){   fmt.Println("Hissssss") } 

only the package app stays and remaining part got deleted on save in vscode.

like image 458
Know Nothing Avatar asked Jan 06 '18 05:01

Know Nothing


People also ask

Is VS code good for go?

In the Go Developer Survey 2020 Results, 41% of respondents chose Visual Studio Code as their most preferred editor for Go. This makes Visual Studio Code the most popular editor for Go developers. Visual Studio Code and the Go extension provide IntelliSense, code navigation, and advanced debugging.

Does VS code save?

By default, VS Code requires an explicit action to save your changes to disk, Ctrl+S. However, it's easy to turn on Auto Save , which will save your changes after a configured delay or when focus leaves the editor. With this option turned on, there is no need to explicitly save the file.

How do I get the path of a file in Vscode?

You can invoke the vscode window property to retrieve the file path or name depending on what you are looking for. This will give you the name of the file open in the current Tab when you execute the command.


2 Answers

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,     } } 
like image 170
akif1 Avatar answered Oct 19 '22 22:10

akif1


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

"[go]": {          "editor.formatOnSave": false,         "editor.codeActionsOnSave": {             "source.organizeImports": false         },     },     "go.formatTool": "gofmt", 
like image 41
LoremIpsum Avatar answered Oct 19 '22 23:10

LoremIpsum