Skip to content

Instantly share code, notes, and snippets.

@jordanorelli
Created March 17, 2012 20:21
Show Gist options
  • Save jordanorelli/2064924 to your computer and use it in GitHub Desktop.
Save jordanorelli/2064924 to your computer and use it in GitHub Desktop.
mysql test (golang)
package main
import (
"io"
"fmt"
"net/http"
"github.com/ziutek/mymysql/mysql"
_ "github.com/ziutek/mymysql/thrsafe"
"runtime"
)
var db mysql.Conn
func indexHandler(w http.ResponseWriter, r *http.Request) {
fmt.Println(runtime.NumCPU(), "\t", runtime.NumGoroutine())
rows, _, err := db.Query("select * from user")
if err != nil {
fmt.Println("Error sending query\n")
http.Error(w, err.Error(), http.StatusInternalServerError)
}
for _, row := range rows {
io.WriteString(w, row.Str(1) + "\n")
}
}
func main() {
runtime.GOMAXPROCS(20)
fmt.Println("establishing db connection...")
db = mysql.New("unix", "", "/tmp/mysql.sock", "pissing", "pissing", "pissing")
err := db.Connect()
if err != nil {
fmt.Println("Error connecting to database.\n")
panic(err)
}
http.HandleFunc("/", indexHandler)
http.ListenAndServe(":8080", nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment