Browse Source

fortune test will dynamically grow the slice rather than reallocating it's size

Patrick Falls 12 years ago
parent
commit
3102fae2fb
1 changed files with 6 additions and 6 deletions
  1. 6 6
      go/src/hello/hello.go

+ 6 - 6
go/src/hello/hello.go

@@ -69,8 +69,7 @@ func dbHandler(w http.ResponseWriter, r *http.Request) {
 }
 
 func fortuneHandler(w http.ResponseWriter, r *http.Request) {
-	// the Fortune table contains 12 rows, and we'll add another Fortune ourselves
-	fortunes := make([]Fortune, 13)
+	var fortunes []Fortune
   
 	stmt := <-fortuneStmts // wait for a connection
   
@@ -80,18 +79,19 @@ func fortuneHandler(w http.ResponseWriter, r *http.Request) {
 		log.Fatalf("Error preparing statement: %s", err)
 	}
   
-	i := 0
 	// Fetch rows
 	for rows.Next() {
 		// get RawBytes from data
-		err = rows.Scan(&fortunes[i].Id, &fortunes[i].Message)
+		fortune := Fortune{}
+		err = rows.Scan(&fortune.Id, &fortune.Message)
 		if err != nil {
 			panic(err.Error())
 		}
-		i++
+		fortunes = append(fortunes, fortune)
+		
 	}
 	fortuneStmts <- stmt // return a connection
-  	fortunes[i].Message = "Additional fortune added at request time."
+  	fortunes = append(fortunes, Fortune{0, "Additional fortune added at request time."})
 	
 	sort.Sort(ByMessage{fortunes})
 	if err := tmpl.Execute(w, map[string]interface{} {"fortunes": fortunes}); err != nil {