0

For the following code, I get the error:

type A struct{
    B_j []B `json:"A"` 
}
type B struct
{
    X string
    Y string

}

func main() {
    xmlFile, _ := os.Open("test.xml")

    b, _ := ioutil.ReadAll(xmlFile)

    var t root
    err2 := xml.Unmarshal(b, &rpc)
    if err2 != nil {
        fmt.Printf("error: %v", err2)
        return
    }

    for _, name := range t.name{
        t := A{B_j : []B{X : name.text, Y: name.type }} // line:#25

        s, _ := json.MarshalIndent(t,"", " ")

    os.Stdout.Write(s)
        }
}

# command-line-arguments
./int2.go:25: undefined: X
./int2.go:25: cannot use name.Text (type string) as type B in array or slice literal
./int2.go:25: undefined: Y
./int2.go:25: cannot use name.type (type string) as type B in array or slice literal

In my output, I am trying to achieve something like this:

{A: {{X:1 ,Y: 2}, {X:2 ,Y: 2}, {X: 2,Y: 2}}}

Struct calling another struct to get the pattern above.

5
  • Yesterday you posted question about XML unmarshall issue of your RPC XML. I have analyzed and answered your question. You didn't accepted the answer, simply you replied "Thanks, that's stupid of me" and deleted the SO question. I suggest not to do that going forward, it is not good for the community. Commented Jun 16, 2017 at 19:25
  • @jeevatkm : that was definitely an unconscious error from my end. Its back now. Apologies! Commented Jun 16, 2017 at 19:27
  • Thanks, no issues. I just thought to bring it your attention. BTW I hope that answer solved your issue. Please accept the answer in SO. Commented Jun 16, 2017 at 19:31
  • Is the quoted file int2.go? Which line is 156? The compiler error is telling you what's wrong, but without any context there's no way for anyone else to help you. Commented Jun 16, 2017 at 19:32
  • @Adrian updated! Commented Jun 16, 2017 at 19:36

1 Answer 1

1

It seems you have problem at this line-

t := A{B_j: []B{X: name.text, Y: name.type }}

You're not creating a slice properly. Try following-

t := A{B_j: []B{{X: name.text, Y: name.type}}}

Let's do it better way-

var bj []B
for _, name := range t.name{
  bj = append(bj, B{X: name.text,Y: name.type})
}

t := A{B_j: bj}
s, _ := json.MarshalIndent(t,"", " ")      
os.Stdout.Write(s)

Sample program with static values https://play.golang.org/p/a2ZDV8lgWP

Note: type is language keyword, do not use it as variable name.

Sign up to request clarification or add additional context in comments.

3 Comments

That fixed the error, but the output is like this: {A : [ {X: 1, Y:2 } ] } , {A : [ {X: 1, Y:2 } ] }, {A : [ {X: 1, Y:2 } ] } I am trying to produce something like: {A: {{X:1 ,Y: 2}, {X:2 ,Y: 2}, {X: 2,Y: 2}}}
should it be var bj []B ?
You're welcome, added golang play link for reference.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.