S
S
Sergey Karbivnichy2021-02-15 21:33:24
go
Sergey Karbivnichy, 2021-02-15 21:33:24

Is it possible to build a project in Go with variables declared but not used?

Decided to try Go today. Porting my project from Python to Go. I noticed such a feature, if a variable is declared in the project, but it is not used, the compiler does not issue a warning, but immediately an error. Of course, the project is not going.
Example:
Going like this:

var files string = "Предположим, тут список файлов"
  fmt.Println(files)
And now it's gone:
var files string = "Предположим, тут список файлов"
  // fmt.Println(files)

./main.go:5:2: imported and not used: "fmt"
Ошибка: процесс завершился с кодом 2.

The same behavior when importing and not using the package:
imported and not used: "fmt"
Since the project already has more than 250 lines of code, such control is already starting to terribly infuriate. After all, you have to debug and temporarily comment on some sections of the code. So, in addition to the code, you also have to comment both variable declarations and imports, and then run through the entire code and uncomment back.

Maybe you can tell the compiler to show warnings instead of errors?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladislav, 2021-02-15
@hottabxp

No, you can't collect.
You can make a feint with your ears (but this is a bad form)

var _ = fmt.Println
var _ = myvar

That is, you need to use it in any case.
In fact, this is a very good feature, it makes the code clean (I always have the same linter enabled in all other languages), and there are no problems with imports when commenting if you use the IDE.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question