我想用我自己的方法擴(kuò)展現(xiàn)有的goquery.Selection類(lèi)型,并能夠從包的選擇器中使用它。我知道我無(wú)法“修補(bǔ)”現(xiàn)有方法——我需要?jiǎng)?chuàng)建一個(gè)新方法。但是如何強(qiáng)制現(xiàn)有的包函數(shù)使用我的新類(lèi)型?我通常缺少什么,或者沒(méi)有“好的”方法可以做到這一點(diǎn),最好使用函數(shù)?package mainimport ( "fmt" "github.com/PuerkitoBio/goquery")type customSelection goquery.Selectionfunc (s *customSelection) CustomMethod() int { return 1}doc.Find("*").Each(func(i int, s *goquery.Selection) { fmt.Println(s.CustomMethod()) // does not works since its still "goquery.Selection" // how do I can get a result with customSelection type here? })
2 回答

大話西游666
TA貢獻(xiàn)1817條經(jīng)驗(yàn) 獲得超14個(gè)贊
您可以使用函數(shù)代替方法:
func customFunc(s *goquery.Selection) int {
return 1
}
...
fmt.Println(customFunc(s))
- 2 回答
- 0 關(guān)注
- 198 瀏覽
添加回答
舉報(bào)
0/150
提交
取消