Home > Backend Development > Golang > How to Handle Unused Values Returned by Go\'s `Exec()` Function?

How to Handle Unused Values Returned by Go\'s `Exec()` Function?

Barbara Streisand
Release: 2024-10-26 07:53:02
Original
734 people have browsed it

How to Handle Unused Values Returned by Go's `Exec()` Function?

Working with Unused Variables in Go's "Exec()" Function

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>
Copy after login

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>
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template