Home  >  Article  >  Backend Development  >  ## How to Handle Unused Return Values from Go\'s Exec() Method?

## How to Handle Unused Return Values from Go\'s Exec() Method?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-10-25 16:26:03213browse

## How to Handle Unused Return Values from Go's Exec() Method?

Addressing Unused Variables in Go

When utilizing the Exec() method for SQL statements, multiple values are returned. However, in cases where the variable representing these values (sqlRes in this scenario) is not required, it results in a compilation error due to its unused status.

To resolve this issue, the blank identifier (_) can be employed. As defined in the language specification, the blank identifier facilitates the dismissal of right-hand side values in an assignment. This enables the evaluation of the Exec() statement without retaining the returned values.

By replacing sqlRes with the blank identifier in the provided code, the compilation error will be eliminated:

<code class="go">stmt, err := db.Prepare("INSERT person SET name=?")
_, err = stmt.Exec(person.Name)</code>

This modification allows the code to successfully execute while ignoring the unnecessary return values.

The above is the detailed content of ## How to Handle Unused Return Values from Go\'s Exec() Method?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn