我有一个结构如下:
Type Post struct{
ID int64
Title string
Content string
}
Run Code Online (Sandbox Code Playgroud)
我使用Go Colly卷曲一个网页来接收数据,我有两个 OnHtml 方法,如下所示:
func main() {
c := colly.NewCollector()
c.OnHTML("p", func(e *colly.HTMLElement) {
Post := Post{
Content: e.Text
}
db.Create(&Post)
})
c.OnHTML("h", func(e *colly.HTMLElement) {
Post := Post{
Title: e.Text
}
db.Create(&Post)
})
c.Visit("http://go-colly.org/")
}
Run Code Online (Sandbox Code Playgroud)
上面的代码运行良好,但这会在数据库中创建两行,如下所示:
+--------------+---------------+---------------+
| id | title | content |
+--------------+---------------+---------------+
| 1 | Hello | Null |
+--------------+---------------+---------------+
| 2 | Null | Mycontent ... |
+--------------+---------------+---------------+
Run Code Online (Sandbox Code Playgroud)
我想创建它:
+--------------+---------------+---------------+
| id | …Run Code Online (Sandbox Code Playgroud)