import ( "database/sql" _ "github.com/go-sql-driver/mysql" ) func main() { db, err := sql.Open("mysql", "user:password@tcp(localhost:3306)/database") if err != nil { panic(err.Error()) } defer db.Close() // Query the database and get a handle to the results rows, err := db.Query("SELECT * FROM users") if err != nil { panic(err.Error()) } defer rows.Close() // Iterate over the rows and fetch the data for rows.Next() { var id int var name string var email string err = rows.Scan(&id, &name, &email) if err != nil { panic(err.Error()) } fmt.Println(id, name, email) } // Check for any errors during iteration err = rows.Err() if err != nil { panic(err.Error()) } }
import ( "database/sql" _ "github.com/lib/pq" ) func main() { db, err := sql.Open("postgres", "user=postgres password=password dbname=database sslmode=disable") if err != nil { panic(err.Error()) } defer db.Close() // Query the database and get a handle to the results rows, err := db.Query("SELECT * FROM large_table") if err != nil { panic(err.Error()) } defer rows.Close() // Iterate over the rows and stream the data for rows.Next() { var id int var name string var data []byte err = rows.Scan(&id, &name, &data) if err != nil { panic(err.Error()) } // Process the row data here processRow(id, name, data) } // Check for any errors during iteration err = rows.Err() if err != nil { panic(err.Error()) } } func processRow(id int, name string, data []byte) { // Process row data here }In both examples, the Rows package library is used for iterating over the rows returned by the database query. The specific implementation details, such as the database driver being used, are handled by the respective database driver libraries imported in the code.