Querying

suggest change

This example is showing how to query a database with database/sql, taking as example a MySql database.

package main

import (
	"database/sql"
	"fmt"
	"log"

	_ "github.com/go-sql-driver/mysql"
)

func main() {
	dsn := "mysql_username:CHANGEME@tcp(localhost:3306)/dbname"

	db, err := sql.Open("mysql", dsn)
	if err != nil {
		log.Fatal(err)
	}
	defer db.Close()

	rows, err := db.Query("select id, first_name from user limit 10")
	if err != nil {
		log.Fatal(err)
	}
	defer rows.Close()

	for rows.Next() {
		var id int
		var username string
		if err := rows.Scan(&id, &username); err != nil {
			log.Fatal(err)
		}
		fmt.Printf("%d-%s\n", id, username)
	}
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents