skip to content
Alvin Lucillo

Ignoring a function parameter

/ 1 min read

Let’s say you have this emit function that calls the handler and passes a context to it. This is the contract as defined by the EventHandler definition. However, in an instance, you don’t need the context in your custom handler. What you can do is ignore the parameter by using _ instead of declaring a variable.

You can name it of course, but it’s safe to ignore it if you won’t use it to avoid accidentally using it. With _, the intent is clear.

type EventHandler func(eventID int, ctx *Context)

type Context struct {
	UserID string
	Trace  string
}

func emit(h EventHandler) {
	ctx := &Context{UserID: "u123", Trace: "t456"}
	h(42, ctx)
}
func main() {
	emit(func(eventID int, _ *Context) {
		fmt.Println("event:", eventID)
	})
}