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

WBOY
Release: 2024-02-06 10:30:11
forward
459 people have browsed it

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
Copy after login

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

html.node

, how to convert it to content? I found out that doing the following...

ndoe, err := htmlquery.loadurl(page.url)

content := content(node)
Copy after login

...can not work. It gave me this error:

cannot convert doc (variable of type *html.Node) to type Content
Copy after login

Correct answer


//m.sbmmt.com/link/b14fba037d119d307e3b6ceb2a9fbb31

If the node is

html.node

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

*html.node

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:

"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:

c := Content(*node) // dereference before conversion
// or
c := *(*Content)(node) // dereference after conversion
Copy after login
//m.sbmmt.com/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!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template