英文:
Create an array of a certain type and reuse it
问题
我遇到了创建GraphQL解析器负载的问题。你可以如何重写代码以返回一个完整的数组?
你被困在c.OnHTML("article", func(e *colly.HTMLElement) {}
中,无法在其外部返回数据。
type Article struct {
Title string `bson:"title"`
Source string `bson:"source"`
Url string `bson:"url"`
Timestamp string `bson:"timestamp"`
}
func (r *RootResolver) News() ([]models.Article, error) {
c := colly.NewCollector(
colly.MaxDepth(2),
colly.Async(),
)
c.Limit(&colly.LimitRule{Parallelism: 10})
articles := []models.Article{}
c.OnHTML("article", func(e *colly.HTMLElement) {
articleModel := []models.Article{
{
Title: e.ChildText("h3 a"),
Source: e.ChildText("a[data-n-tid]"),
Timestamp: e.ChildAttr("div:last-child time", "datetime"),
Url: e.ChildAttr("a[href]", "href"),
},
}
fmt.Println(articleModel)
})
c.Visit(SOMEURLHERE)
c.Wait()
return articles, nil
}
英文:
I'm having trouble creating the payload for my graphql resolver. How could I rewrite this to return a completed array?
I'm stuck inside of c.OnHTML("article", func(e *colly.HTMLElement) {}
and can't return the data outside of it.
type Article struct {
Title string `bson:"title"`
Source string `bson:"source"`
Url string `bson:"url"`
Timestamp string `bson:"timestamp"`
}
func (r *RootResolver) News() ([]models.Article, error) {
c := colly.NewCollector(
colly.MaxDepth(2),
colly.Async(),
)
c.Limit(&colly.LimitRule{Parallelism: 10})
articles := []models.Article{}
c.OnHTML("article", func(e *colly.HTMLElement) {
articleModel := []models.Article{
{
Title: e.ChildText("h3 a"),
Source: e.ChildText("a[data-n-tid]"),
Timestamp: e.ChildAttr("div:last-child time", "datetime"),
Url: e.ChildAttr("a[href]", "href"),
},
}
fmt.Println(articleModel)
})
c.Visit(SOMEURLHERE)
c.Wait()
return articles, nil
}
答案1
得分: 0
如果你需要获取完整的文章列表,你需要在c.OnHTML
函数内部将新的文章追加到articles
切片中,而不是创建一个新的切片。
然后在News()
方法的末尾返回articles
切片。
articles = append(articles, models.Article{
Title: e.ChildText("h3 a"),
Source: e.ChildText("a[data-n-tid]"),
Timestamp: e.ChildAttr("div:last-child time", "datetime"),
Url: e.ChildAttr("a[href]", "href"),
})
在你当前的实现中,articles := []models.Article{}
创建了一个空的models.Article
类型的切片。而在c.OnHTML
内部,你又创建了另一个名为articleModel
的models.Article
类型的切片,并添加了一个元素。这两个切片之间没有任何连接。所以你需要将新的元素追加到articles
切片中,并返回它。
英文:
If you need to get full list of articles, you need to append new article into articles
slice inside of your c.OnHTML
function instead creating new slice.
and return articles
slice end of the News()
Method.
articles = append(articles, models.Article{
Title: e.ChildText("h3 a"),
Source: e.ChildText("a[data-n-tid]"),
Timestamp: e.ChildAttr("div:last-child time", "datetime"),
Url: e.ChildAttr("a[href]", "href"),
})
In your current implementation, articles := []models.Article{}
creates an empty slice of models.Article
type. And inside c.OnHTML
, you creating another slice of models.Article
type named articleModel
with and element. That two slices has no any connection. So you need to append new element to your articles
slice and return it with articles.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论