Package names in import statement are removed automatically in VSCODE for .go file . How to resolve this?

huangapple go评论117阅读模式
英文:

Package names in import statement are removed automatically in VSCODE for .go file . How to resolve this?

问题

我开始在VSCODE编辑器中的.go文件中输入以下代码:

  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "math/rand"
  6. "net/http"
  7. )
  8. func main() {
  9. fmt.Println("Hello World!")
  10. }

当我输入完上述代码后,我保存文件,然后发现我在import语句中输入的内容丢失了,代码变成了以下形式:

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. func main() {
  6. fmt.Println("Hello World!")
  7. }

虽然我知道未使用的导入会自动删除,但我不知道如何解决这个问题。我需要这些包来完成我源代码的其余部分,所以即使在编写代码时,我也包含了这些包名。

在VSCode编辑器中,我应该使用哪个设置来解决这个问题?

英文:

I start typing the code in .go file using VSCODE Editor as below

  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "math/rand"
  6. "net/http"
  7. )
  8. func main() {
  9. fmt.Println("Hello World!")
  10. }

as soon as the above is typed I save the file and then I find that I have lost the statement I have typed in import and the code remains as follows

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. func main() {
  6. fmt.Println("Hello World!")
  7. }

though I am aware that the unused imports are automatically removed, I do not know how to resolve this. I need these packages for the remaining part of my source code and only with that understanding I have included these package names even when I began to write the code.

Which setting should I use to resolve this in VSCode Editor

答案1

得分: 2

在vscode的setting.json文件中,将以下配置设置为false:

  1. "": {
  2. "editor.codeActionsOnSave": {
  3. "source.organizeImports": false
  4. }
  5. }

这样设置后,未使用的导入将不会自动删除。

英文:

> though I am aware that the unused imports are automatically removed, I
> do not know how to resolve this.

In vscode setting.json set this config

  1. "": {
  2. "editor.codeActionsOnSave": {
  3. "source.organizeImports": false
  4. }
  5. }

huangapple
  • 本文由 发表于 2022年9月7日 17:30:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/73633109.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定