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

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

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

问题

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

package main

import (
    "fmt"
    "log"
    "math/rand"
    "net/http"
)

func main() {
    fmt.Println("Hello World!")
}

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

package main

import (
    "fmt"
)

func main() {
    fmt.Println("Hello World!")
}

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

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

英文:

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

package main

import (
    "fmt"
    "log"
    "math/rand"
    "net/http"
)

func main() {
    fmt.Println("Hello World!")
}

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

package main

import (
    "fmt"
)

func main() {
    fmt.Println("Hello World!")
}

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:

"": {
    "editor.codeActionsOnSave": {
        "source.organizeImports": false
    }
}

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

英文:

> 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

"": {
     "editor.codeActionsOnSave": {
            "source.organizeImports": false            
        }
}

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:

确定