I created a new type to add custom methods specific to my application needs
type content html.node
I know I can do this by deriving html.node from a content
var of type
<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">node := html.node(content)</pre><div class="contentsignin">Copy after login</div></div>
However, there is a var of type
, how to convert it to content
?
I found out that doing the following...
ndoe, err := htmlquery.loadurl(page.url) content := content(node)
...can not work. It gave me this error:
cannot convert doc (variable of type *html.Node) to type Content
use:
<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:golang;toolbar:false;">c := content(node)
</pre><div class="contentsignin">Copy after login</div></div>
If the node is
use:
<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:golang;toolbar:false;">c := (*content)(node)
</pre><div class="contentsignin">Copy after login</div></div>
Comments on the specification linked above:
*... it must be enclosed in parentheses if necessary to avoid ambiguity."
If you want
instead of The above is the detailed content of How to convert a type to a new type based on it?. For more information, please follow other related articles on the PHP Chinese website!*content
then you need to dereference the pointer before or after the conversion, for example:
c := Content(*node) // dereference before conversion
// or
c := *(*Content)(node) // dereference after conversion