In Go, the Exec() function used for executing SQL statements returns multiple values, which can sometimes result in the issue of unused variables. This occurs when you don't need or want to use the returned values but still need to declare them for the function to run correctly.
To address this issue, you can use the blank identifier, denoted by an underscore (_). This identifier allows you to ignore right-hand side values in an assignment.
In the example you provided:
<code class="go">stmt, err := db.Prepare("INSERT person SET name=?") sqlRes, err := stmt.Exec(person.Name)</code>
You can replace sqlRes with the blank identifier:
<code class="go">stmt, err := db.Prepare("INSERT person SET name=?") _, err = stmt.Exec(person.Name)</code>
By using the blank identifier, Go will evaluate and ignore the value returned by Exec(), while still allowing the function to execute successfully.
The above is the detailed content of How to Handle Unused Values Returned by Go\'s `Exec()` Function?. For more information, please follow other related articles on the PHP Chinese website!