Bad HTTP response from Google CloudSQL

I am trying to use Google Cloud SQL using go-sql driver. I'm stuck here, I don’t know what is wrong here. This mistake is completely unknown to me.

package hello

import (
  "fmt"
  "net/http"
  "database/sql"
  _ "github.com/go-sql-driver/mysql"
)

func init() {
  http.HandleFunc("/", handler)
}

func handler(w http.ResponseWriter, r *http.Request) {

  const dbUserName = "userName"
  const dbPassword = "password"
  const dbIP = "123.123.231.123"

  db, err := sql.Open("mysql",  dbUserName+":"+dbPassword+"@"+dbIP+":3306/user")


  if err != nil {
    panic(err.Error()) 
  }


  // Query the name 
  rows, err := db.Query("SELECT * FROM user")
  if err != nil {
    panic(err.Error()) 
  }

  fmt.Println(rows)

  defer db.Close()
}

ERROR:

 the runtime process gave a bad HTTP response: ''

 2015/04/12 09:23:36 http: panic serving 127.0.0.1:50091: Default addr  for network '173.194.106.126:3306' unknown
 goroutine 6 [running]:
 net/http.func·011()
 /private/var/folders/00/0v42r000h01000cxqpysvccm003chb/T/appengine/go_appengine/goroot/src/net/http/server.go:1130 +0xbb
 main37089.handler(0x5ad1e0, 0xc208044280, 0xc2080331e0)

Any ideas. Looking at the source source of the go-sql driver, maybe I should set the default address?

+4
source share
1 answer

I do not think that your error has anything to do with the SQL driver. If you read the message, it is related to the http packet server code, and not to anything related to your SQL / read / write connection. Looking at my code and comparing it with basic examples, I am sure that your error is mainly due to the absence of this line;http.ListenAndServe(":8080", nil)

,

func main() {
    http.HandleFunc("/", handler)
    http.ListenAndServe(":8080", nil)
}

, , . ; , , , .

+1

All Articles