My Code :
type Link struct {
Href string `xml:"href,attr"`
}
var html Link = ""
func (s Entry) String() string {
links := string(s.Link)
}
I parsed a whole XML document to get the links and text, Now I want to append all the parsed data in html
variable to construct a nice view on the localhost. But s.Link
can't be converted to string data type maybe because type conversion only supports basic data-types , Any Solutions ?
Live demo : http://play.golang.org/p/7HRHusXLe2
In your case you don't want to append the string
representation of the struct Link
, you just need its Href
field which is already of type string
.
func (s Entry) LinkString() string {
return s.Link.Href
}
Also note that if you use a non-pointer receiver Entry
, your method LinkString()
will receive a copy of the struct. Which in this case is not a problem, it's just a little slower because a copy has to be made.
If your struct gets bigger, it's better to use pointer receiver: *Entry
:
func (s *Entry) LinkString() string {
return s.Link.Href
}
Also note that you don't even need a method to access the URL text because your fields (Entry.Link
and Link.Href
) are exported because they start with an upper-case letter, so you can simply refer to it like this:
// e is of type Entry:
url := e.Link.Href
// url is of type string