package main
import (
"fmt"
"github.com/jmoiron/sqlx"
_ "github.com/lib/pq"
"net/http"
"log"
"encoding/json"
"time"
)
type serverStructV1 struct {
id int `db:"id"`
hostname string `db:"hostname"`
nickname string `db:"nickname"`
locationIp string `db:"location_ip"`
updatedAt time.Time `db:"updatedAt"`
}
func serversHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, world/n")
db, err := sqlx.Open("postgres", "postgres://afdata:afdata@/afdata?sslmode=disable")
if err!= nil{
log.Fatal(err)
}
defer db.Close()
err = db.Ping()
if err != nil {
panic(err.Error())
}
serversdisplay := []serverStructV1{}
err = db.Select(&serversdisplay, "select id,hostname,nickname,location_ip,updatedAt from afdata.serversV1")
if err != nil {
log.Fatal(err)
}
}
当我运行它时,如果我改变
,它会在> 1列(5)上显示on-struct dest类型结构 err = db.Select(&serversdisplay, "select id,hostname,nickname,location_ip,updatedAt from afdata.serversV1")
进入
err = db.Select(&serversdisplay, "select id from afdata.serversV1")
结果变为null。 我使用psql测试选择查询,它可以返回NOT NULL结果。但即使列匹配,它也会返回null。它连接到数据库,但没有得到查询结果。