Pessoal,
Estou tendo problema ao tentar acessar o banco de dados. O primeiro teste de conexão funcionou, mas o tentar exibir os dados, da o seguinte erro no console.
2021/12/01 01:30:07 http: panic serving [::1]:54916: dial tcp 127.0.0.1:5432: connect: connection refused
goroutine 34 [running]:
net/http.(*conn).serve.func1(0xc000156000)
/usr/lib/go-1.13/src/net/http/server.go:1767 +0x139
panic(0x7ad680, 0xc000134270)
/usr/lib/go-1.13/src/runtime/panic.go:679 +0x1b2
main.index(0x8d2520, 0xc0001320e0, 0xc000176000)
/mnt/c/Users/Evandro/Documents/workspace/golang/appweb/main.go:41 +0x585
net/http.HandlerFunc.ServeHTTP(0x85eaa0, 0x8d2520, 0xc0001320e0, 0xc000176000)
/usr/lib/go-1.13/src/net/http/server.go:2007 +0x44
net/http.(*ServeMux).ServeHTTP(0xb5f520, 0x8d2520, 0xc0001320e0, 0xc000176000)
/usr/lib/go-1.13/src/net/http/server.go:2387 +0x1bd
net/http.serverHandler.ServeHTTP(0xc000132000, 0x8d2520, 0xc0001320e0, 0xc000176000)
/usr/lib/go-1.13/src/net/http/server.go:2802 +0xa4
net/http.(*conn).serve(0xc000156000, 0x8d30e0, 0xc00012c080)
/usr/lib/go-1.13/src/net/http/server.go:1890 +0x875
created by net/http.(*Server).Serve
/usr/lib/go-1.13/src/net/http/server.go:2928 +0x384
2021/12/01 01:30:07 http: panic serving [::1]:54918: dial tcp 127.0.0.1:5432: connect: connection refused
goroutine 35 [running]:
net/http.(*conn).serve.func1(0xc0001560a0)
/usr/lib/go-1.13/src/net/http/server.go:1767 +0x139
panic(0x7ad680, 0xc0000120a0)
/usr/lib/go-1.13/src/runtime/panic.go:679 +0x1b2
main.index(0x8d2520, 0xc0001e4000, 0xc000176100)
/mnt/c/Users/Evandro/Documents/workspace/golang/appweb/main.go:41 +0x585
net/http.HandlerFunc.ServeHTTP(0x85eaa0, 0x8d2520, 0xc0001e4000, 0xc000176100)
/usr/lib/go-1.13/src/net/http/server.go:2007 +0x44
net/http.(*ServeMux).ServeHTTP(0xb5f520, 0x8d2520, 0xc0001e4000, 0xc000176100)
/usr/lib/go-1.13/src/net/http/server.go:2387 +0x1bd
net/http.serverHandler.ServeHTTP(0xc000132000, 0x8d2520, 0xc0001e4000, 0xc000176100)
/usr/lib/go-1.13/src/net/http/server.go:2802 +0xa4
net/http.(*conn).serve(0xc0001560a0, 0x8d30e0, 0xc00012c140)
/usr/lib/go-1.13/src/net/http/server.go:1890 +0x875
created by net/http.(*Server).Serve
/usr/lib/go-1.13/src/net/http/server.go:2928 +0x384
2021/12/01 01:30:07 http: panic serving [::1]:54924: dial tcp 127.0.0.1:5432: connect: connection refused
goroutine 9 [running]:
net/http.(*conn).serve.func1(0xc000216000)
/usr/lib/go-1.13/src/net/http/server.go:1767 +0x139
panic(0x7ad680, 0xc000012140)
/usr/lib/go-1.13/src/runtime/panic.go:679 +0x1b2
main.index(0x8d2520, 0xc0001e40e0, 0xc0001fe100)
/mnt/c/Users/Evandro/Documents/workspace/golang/appweb/main.go:41 +0x585
net/http.HandlerFunc.ServeHTTP(0x85eaa0, 0x8d2520, 0xc0001e40e0, 0xc0001fe100)
/usr/lib/go-1.13/src/net/http/server.go:2007 +0x44
net/http.(*ServeMux).ServeHTTP(0xb5f520, 0x8d2520, 0xc0001e40e0, 0xc0001fe100)
/usr/lib/go-1.13/src/net/http/server.go:2387 +0x1bd
net/http.serverHandler.ServeHTTP(0xc000132000, 0x8d2520, 0xc0001e40e0, 0xc0001fe100)
/usr/lib/go-1.13/src/net/http/server.go:2802 +0xa4
net/http.(*conn).serve(0xc000216000, 0x8d30e0, 0xc00002e240)
/usr/lib/go-1.13/src/net/http/server.go:1890 +0x875
created by net/http.(*Server).Serve
/usr/lib/go-1.13/src/net/http/server.go:2928 +0x384
Segue meu código.
package main
import (
"database/sql"
"net/http"
"text/template"
_ "github.com/lib/pq"
)
func conectaComBancoDeDados() *sql.DB {
conexao := "user=postgres dbname=appweb password=123456 host=localhost sslmode=disable"
db, err := sql.Open("postgres", conexao)
if err != nil {
panic(err.Error())
}
return db
}
type Produto struct {
Id int
Nome string
Descricao string
Preco float64
Quantidade int
}
var temp = template.Must(template.ParseGlob("templates/*.html"))
func main() {
http.HandleFunc("/", index)
http.ListenAndServe(":8000", nil)
}
func index(w http.ResponseWriter, r *http.Request) {
db := conectaComBancoDeDados()
selectDeTodosOsProdutos, err := db.Query("select * from produtos")
if err != nil {
panic(err.Error())
}
p := Produto{}
produtos := []Produto{}
for selectDeTodosOsProdutos.Next() {
var id, quantidade int
var nome, descricao string
var preco float64
err = selectDeTodosOsProdutos.Scan(&id, &nome, &descricao, &preco, &quantidade)
if err != nil {
panic(err.Error())
}