使用 golang gin rest api,我创建标题、正文、日期、标题和内容字段,发送给邮递员并保存数据库。 post 操作运行成功,但是当我收到数据时,出现如下错误:“error”:“无法导入作业” get 不起作用,但 post 起作用 代码部分如下
main.go:
type Job struct {
ID int `db:"id" json:"id"`
Title string `db:"title" json:"title"`
Body string `db:"body" json:"body"`
Date time.Time `db:"date" json:"date"`
Titles [4]string `db:"titles" json:"titles"`
Contents [4]string `db:"contents" json:"contents"`
}
func main() {
r.POST("/job", func(c *gin.Context) {
var job Job
if err := c.ShouldBindJSON(&job); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
// insert job into database
query := "INSERT INTO table (title, body, titles, contents ,date) VALUES ($1, $2, $3, $4, $5) RETURNING id"
var id int
err := db.QueryRow(query, job.Title, job.Body, pq.Array(job.Titles), pq.Array(job.Contents), time.Now()).Scan(&id)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to create job"})
return
}
job.ID = id
c.JSON(http.StatusOK, job)
})
r.GET("/jobs", func(c *gin.Context) {
// retrieve all jobs from database
rows, err := db.Query("SELECT * FROM table")
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to retrieve jobs"})
return
}
defer rows.Close()
// iterate over rows and store in slice of Jobs
jobs := []Job{}
for rows.Next() {
var job Job
err := rows.Scan(&job.ID, &job.Title, &job.Body, &job.Date, &job.Titles, &job.Contents)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to retrieve jobs"})
return
}
jobs = append(jobs, job)
}
c.JSON(http.StatusOK, jobs)
})`
我已经使用以下代码成功管理了您的需求。我将分享完整的
main.go
文件,以便您也可以看到我使用了哪些软件包。
main.go
文件
package main
import (
"database/sql"
"net/http"
"time"
"github.com/gin-gonic/gin"
"github.com/lib/pq"
_ "github.com/lib/pq"
)
type Job struct {
ID int `db:"id" json:"id"`
Title string `db:"title" json:"title"`
Body string `db:"body" json:"body"`
Date time.Time `db:"date" json:"date"`
Titles [4]string `db:"titles" json:"titles"`
Contents [4]string `db:"contents" json:"contents"`
}
func main() {
dsn := "host=localhost user=postgres password=postgres dbname=postgres port=5432 sslmode=disable"
db, err := sql.Open("postgres", dsn)
if err != nil {
panic(err)
}
r := gin.Default()
r.POST("/job", func(c *gin.Context) {
var job Job
if err := c.ShouldBindJSON(&job); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
// insert job into database
query := "INSERT INTO jobs (title, body, titles, contents ,date) VALUES ($1, $2, $3, $4, $5) RETURNING id"
var id int
err := db.QueryRow(query, job.Title, job.Body, pq.Array(job.Titles), pq.Array(job.Contents), time.Now()).Scan(&id)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to create job"})
return
}
job.ID = id
c.JSON(http.StatusOK, job)
})
r.GET("/jobs", func(c *gin.Context) {
// retrieve all jobs from database
rows, err := db.Query("SELECT * FROM jobs")
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to retrieve jobs"})
return
}
defer rows.Close()
// iterate over rows and store in slice of Jobs
jobs := []Job{}
for rows.Next() {
var job Job
err := rows.Scan(&job.ID, &job.Title, &job.Body, &job.Date, pq.Array(job.Titles[:]), pq.Array(job.Contents[:]))
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to retrieve jobs"})
return
}
jobs = append(jobs, job)
}
c.JSON(http.StatusOK, jobs)
})
r.Run()
}
让我回顾一下这些变化:
table
切换为 jobs
。Scan
处理程序的 GET
部分中,我做了这两项更改:
pq.Array
包提供的pb
类型来扫描Titles
和Content
字段[4]string
类型的数组转换为切片。 [:]
。因此,您不必使用运算符 &
的地址,因为切片是通过引用传递的。通过这个技巧,它可以扫描值并填充 Job
结构。如果这对您有帮助,请告诉我,谢谢!