I create a new type to add custom methods specific for my app’s needs
type Content html.Node
I know I can derivate a html.Node from a content var of type Content doing this
node := html.Node(content)
But, having a var of type html.Node, how can I convert to a Content ?
I discovered that doing the following …
ndoe, err := htmlquery.LoadURL(page.url)
content := Content(node)
… cannot work. It gives me this error:
cannot convert doc (variable of type *html.Node) to type Content
>Solution :
https://go.dev/ref/spec#Conversions
If node is html.Node use:
c := Content(node)
If node is *html.Node use:
c := (*Content)(node)
Note from the above linked spec: "If the type starts with the operator * … it must be parenthesized when necessary to avoid ambiguity."
And if you want Content instead of *Content then you need to dereference the pointer, either before conversion or after, e.g.:
c := Content(*node) // dereference before conversion
// or
c := *(*Content)(node) // dereference after conversion
https://go.dev/play/p/zAu4H1R2D-L