notexOPENOverview
Notex/Languages/Ignoring Returned Values
03.05LESSON 05 OF 18 · 1 MIN READ

Ignoring Returned Values

Always free to read

What if you call a function and don’t want to use all the returned values? As covered in “Unused Variables”, Go does not allow unused variables. If a function returns multiple values, but you don’t need to read one or more of them, assign the unused values to the name . For example, if you weren’t going to read remainder, you would write the assignment as result, , err := divAndRemainder(5, 2).

Surprisingly, Go does let you implicitly ignore all of the return values for a function. You can write divAndRemainder(5,2), and the returned values are dropped. In fact, you’ve actually been doing this since the earliest example in these notes: fmt.Println returns two values, but it is idiomatic to ignore them. In almost all other cases, you should make it explicit that you are ignoring return values by using underscores.

Tip

Use _ whenever you don’t need to read a value that’s returned by a function.

Progress is saved on this device.
KEEP EXPLORING

One note leads to another.

Back to the library →
⌕
Search across the entire library ⌘ K