Browse Source

Merge pull request #130 from tarndt/master

Go: Lite cleanup of source code
Patrick Falls 12 years ago
parent
commit
2168d8ed06
1 changed files with 35 additions and 34 deletions
  1. 35 34
      go/src/hello/hello.go

+ 35 - 34
go/src/hello/hello.go

@@ -1,67 +1,68 @@
 package main
 package main
 
 
 import (
 import (
-	"encoding/json"
-	"net/http"
-	"runtime"
-	"strconv"
-
 	"database/sql"
 	"database/sql"
+	"encoding/json"
 	_ "github.com/go-sql-driver/mysql"
 	_ "github.com/go-sql-driver/mysql"
 	"log"
 	"log"
 	"math/rand"
 	"math/rand"
+	"net/http"
+	"runtime"
+	"strconv"
 )
 )
 
 
-const DB_ROWS = 10000
-
-var db *sql.DB
-var q *sql.Stmt
-
 type MessageStruct struct {
 type MessageStruct struct {
 	Message string
 	Message string
 }
 }
+
 type World struct {
 type World struct {
 	Id           uint16 `json:"id"`
 	Id           uint16 `json:"id"`
 	RandomNumber uint16 `json:"randomNumber"`
 	RandomNumber uint16 `json:"randomNumber"`
 }
 }
 
 
-func hello(w http.ResponseWriter, r *http.Request) {
+const (
+	DB_CONN_STR   = "benchmarkdbuser:benchmarkdbpass@tcp(localhost:3306)/hello_world?charset=utf8"
+	DB_SELECT_SQL = "SELECT * FROM World where id = ?;"
+	DB_ROWS       = 10000
+)
+
+var (
+	db    *sql.DB
+	query *sql.Stmt
+)
+
+func main() {
+	runtime.GOMAXPROCS(runtime.NumCPU())
+	var err error
+	if db, err = sql.Open("mysql", DB_CONN_STR); err != nil {
+		log.Fatalf("Error opening database: %s", err)
+	}
+	if query, err = db.Prepare(DB_SELECT_SQL); err != nil {
+		log.Fatalf("Error preparing statement: %s", err)
+	}
+	http.HandleFunc("/json", jsonHandler)
+	http.HandleFunc("/db", dbHandler)
+	http.ListenAndServe(":8080", nil)
+}
+
+func jsonHandler(w http.ResponseWriter, r *http.Request) {
 	w.Header().Set("Content-Type", "application/javascript")
 	w.Header().Set("Content-Type", "application/javascript")
 	j, _ := json.Marshal(&MessageStruct{"Hello, world"})
 	j, _ := json.Marshal(&MessageStruct{"Hello, world"})
 	w.Header().Set("Content-Length", strconv.Itoa(len(j)))
 	w.Header().Set("Content-Length", strconv.Itoa(len(j)))
 	w.Write(j)
 	w.Write(j)
 }
 }
 
 
-func dbh(w http.ResponseWriter, r *http.Request) {
+func dbHandler(w http.ResponseWriter, r *http.Request) {
 	qnum := 1
 	qnum := 1
-	qnumString := r.URL.Query().Get("queries")
-	if len(qnumString) != 0 {
-		qnum, _ = strconv.Atoi(qnumString)
+	if qnumStr := r.URL.Query().Get("queries"); len(qnumStr) != 0 {
+		qnum, _ = strconv.Atoi(qnumStr)
 	}
 	}
 	ww := make([]World, qnum)
 	ww := make([]World, qnum)
 	for i := 0; i < qnum; i++ {
 	for i := 0; i < qnum; i++ {
-		q.QueryRow(rand.Intn(DB_ROWS)+1).Scan(&ww[i].Id, &ww[i].RandomNumber)
+		query.QueryRow(rand.Intn(DB_ROWS)+1).Scan(&ww[i].Id, &ww[i].RandomNumber)
 	}
 	}
 	w.Header().Set("Content-Type", "application/javascript")
 	w.Header().Set("Content-Type", "application/javascript")
 	j, _ := json.Marshal(ww)
 	j, _ := json.Marshal(ww)
 	w.Header().Set("Content-Length", strconv.Itoa(len(j)))
 	w.Header().Set("Content-Length", strconv.Itoa(len(j)))
 	w.Write(j)
 	w.Write(j)
 }
 }
-
-func main() {
-	var err error
-	runtime.GOMAXPROCS(runtime.NumCPU())
-
-	db, err = sql.Open("mysql", "benchmarkdbuser:benchmarkdbpass@tcp(localhost:3306)/hello_world?charset=utf8")
-	if err != nil {
-		log.Fatalf("Error opening database: %s", err)
-	}
-	q, err = db.Prepare("SELECT * FROM World where id = ?;")
-	if err != nil {
-		log.Fatalf("Error preparing statement: %s", err)
-	}
-
-	http.HandleFunc("/json", hello)
-	http.HandleFunc("/db", dbh)
-	http.ListenAndServe(":8080", nil)
-}