Created
March 17, 2012 20:21
-
-
Save jordanorelli/2064924 to your computer and use it in GitHub Desktop.
mysql test (golang)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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