vscode removing Imports when saved file

Asked

Viewed 181 times

1

I have a very simple code in GO. Using vs code, when saving the file I’m working on, the editor removes the import "math"

Follows the code

package main

import "math"

func main() {
    a := Sqrt(float64(60))
}

When saved the file, the vs code formatter for the language GO is removing the import math, but it is being used (in the Sqrt function).

Why is this happening? It’s a code error or I need to configure something in vs code?

2 answers

1


It is not VS code that removes import but the compiler from GO, in theory GO removes all imports that you are not using for performance reasons.

When you import from a library, you have to inform that you will be using the same.

package main

import "fmt"

func main() {
         //bem aqui utilizando a biblioteca FMT
    a := fmt.Println("Hello")
}

1

I figured out how to solve the problem, the prefix was missing math in front of the function Sqrt. Anyway, problems of beginner in language.

The whole code would look like this:

package main

import "math"

func main() {
    a := math.Sqrt(float64(60)) 
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.