Discard values with the blank identifier in Go
An intentional placeholder
The single underscore _ is the blank identifier. It is a placeholder rather than a variable whose value you can read. In a multiple assignment or declaration it discards a value that you do not need. Here the second value is unnecessary for the task.
package main
import "fmt"
func main() { name, _ := "Ada", 23 fmt.Println(name)}The output is Ada. The name is declared, while the integer is discarded. You cannot later write fmt.Println(_) because _ does not provide a readable value. _ alone cannot supply the new name required by :=.
Discard only what is safe to ignore
package main
import "fmt"
func main() { _, err := fmt.Println("Saved") fmt.Println(err == nil)}The first call prints Saved and returns a byte count and an error. This example discards the count but keeps the error; the comparison err == nil asks whether no error was returned. It prints true on a successful write. Real programs should handle errors rather than silently discarding them; the error-handling chapter will teach that. Discarding a result does not stop evaluation or side effects of the right-hand expression.
Try it locally
Use a short declaration to keep the second value, 18, from the pair "unused", 18 and discard the first. Name the variable points and print it. Expected: 18.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Tokens and Declarations 07: Discard values with the blank identifier in Go
Check the lesson objective before moving on.
What does _ represent in x, _ := 1, 2?
Does assigning a function result to _ prevent that function from running?