GO错误将xml结构写入文件

时间:2015-11-10 16:36:47

标签: xml struct go

目前我的控制台输出格式为[{a} {b} {c} {d}]

我希望将此输出写入文件而不是fmt.Println(a),但每次遇到错误时都会显示:

cannot use a (type []Group) as type []byte in argument to ioutil.WriteFile' or similar
cannot use a (`type []Group`) as type `io.Writer` in argument to `fmt.Fprint`:
[]Group does not implement io.Writer (missing Write method)).

我是否需要将[]group转换为其他类型?我试过了json.Marshall(group),但结果却一样。

package main

import (
  "fmt"
  "net/http"
  "io/ioutil"
  "encoding/xml"
)

type Bedework struct {
  XMLName xml.Name `xml:"bedework"`
  Groups []Group `xml:"groups>group"`
}

type Group struct {
  Name string `xml:"name"`
}

func main() {
  response, err := http.Get("http://localhost:3000/cal/?noxslt=yes")
  if err != nil {
    fmt.Println("Error opening file:", err)
    return
  } else {
    defer response.Body.Close()

    contents, _ := ioutil.ReadAll(response.Body)

    var b Bedework
    var g []Group
    xml.Unmarshal(contents, &b)
    for _, group := range b.Groups {
        g = append(g, group)
    }
  fmt.Println(g)
  }
}

0 个答案:

没有答案