go言語 「declared but not used」の解決法

  • 作成日 2021.04.23
  • 更新日 2022.11.02
  • go
go言語 「declared but not used」の解決法

go言語で、エラー「declared but not used」がでた場合の解決法を記述してます。go言語のバージョンは1.15.4を使用してます。

環境

  • OS windows10 pro 64bit
  • go言語 1.15.4

エラー内容

以下のコードで発生します。

package main

func main() {

	num := 1

}

エラーメッセージ

num declared but not used

解決法

使用してないというエラーなので、「アンダースコア変数」を使用すればエラーは解消されます。

package main

func main() {

	num := 1

	_ = num

}