go - In golang how do I encode an xml parameter containing a space? -


i have xml api construct need use (this construct not defined me , cannot change it):

<path><farmer id="me7"/></path> 

in go have:

type path struct {   xmlname xml.name `xml:"path"`   farmerid string `xml:"farmer id,attr"` }  pmux := &path{farmerid: "me7"} 

however go encodes pmux , prints this:

<path xmlns:farmer="farmer" farmer:id="me7" </path> 

what want this:

<path><farmer id="me7"/> </path> 

how can achieve this?

thx

the xml invalid, if need come out that, use regular expression fix afterward. here example.

i assuming want open tag valid <path farmer id="me7"></path>, instead of not having open tag valid posted <path farmer id="me7" </path>, either way doable regex.

btw, question inconsistent want. start wanting <path><farmer id="me7"></path>, @eugenioy 's answer accommodate. end "what want this: <path farmer id="me7" </path>". answer geared toward.

https://play.golang.org/p/a-sjhigfzw

package main  import (     "encoding/xml"     "fmt"     "regexp" )  type path struct {     xmlname  xml.name `xml:"path"`     farmer   string   `xml:"farmer,attr"`     farmerid string   `xml:"id,attr"` }  func main() {     path := &path{         farmerid: "me7",     }     data, err := xml.marshal(path)     if err != nil {         fmt.println(err)         return     }      strdata := string(data)      // fix regex     reg := regexp.mustcompile(`(farmer)(="")`)     strdata = reg.replaceallstring(strdata, "$1")     fmt.println(strdata) // <path farmer id="me7"></path> } 

Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -