Sitemap
Devpriya Shivani

Devpriya Shivani

Go Blank Identifier

3 min readJul 22, 2024

--

Press enter or click to view image in full size
Blank Identifier in Go

In Go, the blank identifier is represented by an underscore (_). It's a special identifier that can be used in various scenarios where a variable is required syntactically, but its value doesn't need to be used. Here are some common uses of the blank identifier:

1. Ignoring a Value

When you only care about certain values a function returns and want to ignore others, you can use the blank identifier to discard the unwanted values.

package main

import (
"fmt"
"os"
)

func main() {
file, err := os.Open("Vedanta Meets Feminism and Sublates It")
if err != nil {
fmt.Println("Error:", err)
return
}
defer file.Close()

// Read some bytes from the file
data := make([]byte, 100)
byteSliceSize, err = file.Read(data)
if err != nil {
fmt.Println("Error:", err)
return
}

fmt.Println("File content:", string(data))
}

Output:

byteSliceSize declared and not used

If you do not intend to use byteSliceSize, the Go compiler will issue a warning, indicating that you have declared a variable but are not using it. This can clutter your code and reduce readability. To avoid this, you can use the blank identifier _ .

package main

import (
"fmt"
"os"
)

func main() {
file, err := os.Open("Vedanta Meets Feminism and Sublates It")
if err != nil {
fmt.Println("Error:", err)
return
}
defer file.Close()

// Read some bytes from the file
data := make([]byte, 100)
_, err = file.Read(data)
if err != nil {
fmt.Println("Error:", err)
return
}

fmt.Println("File content:", string(data))
}

Output:

File content: Vedanta Meets Feminism and Sublates It

From the Vedantic perspective, true feminism emerges when a

Here, the blank identifier tells the compiler that you intentionally discard the byteSliceSize value. This improves code clarity and avoids compiler warnings.

Get Devpriya Shivani’s stories in your inbox

Join Medium for free to get updates from this writer.

2. Ignoring an Index or Value in a Loop

When you iterate over a slice or map and don’t need the index or value, you can use the blank identifier to ignore it.

package main

import "fmt"

func main() {
values := []int{10, 20, 30, 40, 50}

for _, value := range values {
fmt.Println(value)
}
}

Output:

10
20
30
40
50

3. Importing a Package for Side Effects

Sometimes, you may want to import a package solely for its side effects, such as initialization. In this case, you can use the blank identifier to import the package without using any of its exported identifiers.

package main

import (
_ "net/http/pprof"
)

func main() {
// The package "net/http/pprof" is imported for its side effects only
}

Output:

5. Interface Implementations

When implementing an interface, you might need to fulfill a method signature but don’t need to use the parameters. The blank identifier can help with that.

package main

import "fmt"

type MyInterface interface {
MyMethod(int)
}

type MyStruct struct{}

func (m MyStruct) MyMethod(_ int) {
fmt.Println("MyMethod called")
}

func main() {
var i MyInterface = MyStruct{}
i.MyMethod(123)
}

Output:

MyMethod called

Conclusion

The blank identifier is a versatile tool in Go that helps you avoid unused variable errors, handle function return values selectively, and more. Understanding how to use it effectively can make your Go code cleaner and more efficient.

--

--