Home > Article > Backend Development > How to convert a type to a new type based on it?
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
<pre class="brush:php;toolbar:false;">node := html.node(content)</pre>
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 ContentCorrect answer
use:
<pre class="brush:golang;toolbar:false;">c := content(node)
</pre>
If the node is
use:
<pre class="brush:golang;toolbar:false;">c := (*content)(node)
</pre>
Comments on the specification linked above:
*... it must be enclosed in parentheses if necessary to avoid ambiguity."
If you want
instead of *content
then you need to dereference the pointer before or after the conversion, for example:
<pre class="brush:golang;toolbar:false;">c := Content(*node) // dereference before conversion
// or
c := *(*Content)(node) // dereference after conversion
</pre>
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!