In this tutorial, we will walk through some examples of avoiding error "declared and not used" in Golang. If a variable is defined in the Go programming language but not used in the code, the compiler will display the error "declared and not used" We must eliminate any unnecessary packages or variables from the code in accordance with best practices for programming. Memory, compilation, and execution time will be saved.
You can avoid removing those unnecessary variables or packages by using the blank identifier (_).
Take a look at the examples below:
Example 1: Error - "imported and not used"
Here we have a sample go code:
package main
import (
"fmt"
"encoding/json"
)
func main() {
name := "GoLinuxCloud"
age := 1
fmt.Println("My name is:", name, "and my age is: ", age)
}
In the above code, two packages <span class="w3-codespan">fmt</span>
and encoding/json
are imported but encoding/json
was not used. So the following error will return:
# command-line-arguments
.\milestone8.go:5:2: imported and not used: "encoding/json"
Fix imported and not used
We ca use a blank identifier (_) before the package name that will not be used as following code:
package main
import (
"fmt"
_ "encoding/json"
)
func main() {
name := "GoLinuxCloud"
age := 1
fmt.Println("My name is:", name, "and my age is:", age)
}
Output:
My name is: GoLinuxCloud and my age is: 1
Example 2: Error - "declared and not used"
Here we have another go code:
package main
import (
"fmt"
)
func main() {
page := "GolinuCloud"
author := "Anonymous Author"
fmt.Println("Name is:", page)
}
In the above code, two variables page
and author
are declared but author
was not used. So the following error will return.
# command-line-arguments
.\milestone8.go:9:2: author declared but not used
Fix declared but not used
To avoid this error we can assign the variable to a blank identifier (_) as the following code:
package main
import (
"fmt"
)
func main() {
page := "GolinuCloud"
author := "Anonymous Author"
fmt.Println("Page is:", page)
_ = author
}
Output:
Page is: GolinuCloud
Example 3: Variable declared and used but still getting "declared but not used"
In the below example, the variable is used but the program still arise declared but not used error:
package main
import "fmt"
func main(){
var boolStr string
if false {
boolStr := "False"
}else{
boolStr := "True"
}
fmt.Println(boolStr) // variable used
}
Because the variable boolStr
is being re-declared in the if-else
block of the code, it returns the error. Any variables declared using :=
 are local
in the go programming language, meaning they can only be utilized inside the specific block or function. The code fails because the variable is not used in the specific if-else block range. To solve this problem, you just need to assign new values to the global variable boolStr
 instead of declaring them again, as shown below:
package main
import "fmt"
func main(){
var boolStr string // declared global variable
if false {
boolStr = "False" // asign value, not declare again
}else{
boolStr = "True"
}
fmt.Println(boolStr) // variable used
}
Output:
True
Summary
In this article, I have provided some example of how to eliminate declare but not used error. The presence of an unused variable may indicate a bug, while unused imports just slow down compilation. Accumulate enough unused imports in your code tree and things can get very slow. For these reasons, Go allows neither.
To avoid these compilation errors I recommend using Visual Studio Code for GO IDE which will warn you about these errors even before you compile and run your code.
References
https://go.dev/doc/effective_go#blank