Home  >  Article  >  Backend Development  >  How to convert a type to a new type based on it?

How to convert a type to a new type based on it?

WBOY
WBOYforward
2024-02-06 10:30:11417browse

How to convert a type to a new type based on it?

Question content

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

html.node

, 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

Correct answer


https://www.php.cn/link/b14fba037d119d307e3b6ceb2a9fbb31

If the node is

html.node

use: <pre class="brush:golang;toolbar:false;">c := content(node) </pre> If the node is

*html.node

use: <pre class="brush:golang;toolbar:false;">c := (*content)(node) </pre> Comments on the specification linked above:

"If a type begins with the operator

*... it must be enclosed in parentheses if necessary to avoid ambiguity." If you want

content

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>

https://www.php.cn/link/fb9d433088a21464e7d634c4e190b31a

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!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete