Parameterized Statements
The following code example shows how to bind parameters to create parameterized statements.
Single-Use Statements
The Query and Exec functions both accept additional parameters for binding query parameters to values.
rows, _ := db.Query("SELECT City, Country FROM Contacts WHERE VID = ?", "123456789") defer rows.Close() for rows.Next() { var ( City string Country string ) rows.Scan(&City, &Country) fmt.Printf("City = %s, Country = %s\n", City, Country) }
Reusable Statements
The Prepare function creates prepared Stmt objects, which can be re-used across multiple Query and Exec calls.
stmt, _ := db.Prepare("SELECT City, Country FROM Contacts WHERE VID = ?") defer stmt.Close() rows, _ := stmt.Query("123456789 1") defer rows.Close() for rows.Next() { var ( City string Country string ) rows1.Scan(&City, &Country) fmt.Printf("City = %s, Country = %s\n", City, Country) } rows, _ = stmt.Query("123456789 2") defer rows.Close() for rows.Next() { var ( City string Country string ) rows2.Scan(&City, &Country) fmt.Printf("City = %s, Country = %s\n", City, Country) }