Home >Backend Development >Golang >How to Ignore Unused Return Values from Go\'s Exec() Method?
In Go, the Exec() method for SQL statements returns multiple values, one of which indicates the number of rows affected. However, declaring a variable to receive this value may be unnecessary, leading to compilation errors due to unused variables.
To address this issue, Go provides the blank identifier (_), which allows you to ignore specific return values in an assignment statement.
Consider the以下 code:
<code class="go">stmt, err := db.Prepare("INSERT person SET name=?") _, err = stmt.Exec(person.Name)</code>
In this example, the variable sqlRes originally declared to receive the result is replaced with the blank identifier. This instructs the compiler to ignore the return value, resolving the compilation error while effectively executing the SQL statement.
According to the language specification, the blank identifier provides a means to discard right-hand side values in assignments:
_ = x // evaluate x but ignore it x, _ = f() // evaluate f() but ignore second result value
By utilizing the blank identifier, you can execute SQL statements without declaring and assigning unused return values, ensuring clean and efficient code.
The above is the detailed content of How to Ignore Unused Return Values from Go\'s Exec() Method?. For more information, please follow other related articles on the PHP Chinese website!