从Google CloudSQL返回的HTTP响应有问题。

huangapple go评论94阅读模式
英文:

bad HTTP response from Google CloudSQL

问题

我正在尝试使用go-sql-driver连接Google Cloud SQL。我在这里遇到了问题,不知道出了什么错。这个错误对我来说完全是未知的。

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()
}

错误信息:

运行时进程返回了错误的HTTP响应: ''

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)

有什么想法吗?看了go-sql-driver源代码,也许我应该设置一个默认地址?

英文:

I'm trying hand at Google Cloud SQL using go-sql-driver. I'm stuck here I don't know whats wrong here. This error is totally unknown for 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 source of go-sql-driver source maybe I should set a default address?

答案1

得分: 1

我不认为你的错误与SQL驱动程序有任何关系。如果你读一下错误信息,你会发现它是由http包的服务器代码引起的,与你的SQL连接/读取/写入等无关。根据你的代码和基本示例的对比,我相当确定你的错误是由于在main函数中缺少这一行代码引起的:http.ListenAndServe(":8080", nil)

基本上,将你的main函数改为:

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

这样,错误可能会消失。免责声明:这并不意味着你的程序会神奇地工作,只是没有这行代码它永远不会工作。

英文:

I don't think your error has anything to do with the SQL driver. If you read the message it is arising from the http packages server code, not from anything having to do with your SQL connection/reading/writing ect. Looking at your code and comparing it to basic examples I'm fairly sure your error is due to lack of this line in main; http.ListenAndServe(":8080", nil)

Basically, change your main to

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

And that error will probably go away. Disclaimer; that doesn't mean your program will magically work, only that it will never work without it.

huangapple
  • 本文由 发表于 2015年4月12日 17:33:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/29587973.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定