comma ok idiom

The Go pattern of receiving a second true-or-false result, as in value, ok := m[key], to learn whether an operation really found something.

In more detail

For a map, ok is false when the key is missing, which separates absent from a stored zero. The same form checks type assertions, os.LookupEnv and receives from a closed channel. Use it whenever a zero value could be a real answer.

Example

port, ok := ports["ssh"]
if !ok {
	fmt.Println("ssh is absent")
}

Related terms

Taught in

References