0Pricing
Go Academy · Lesson

encoding/xml

Work with XML.

encoding/xml is a free Go Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Working with XML

XML is a verbose, tag-based format still common in legacy systems and configs. Go's encoding/xml package marshals and unmarshals it, mirroring the json package.

Marshal to XML

xml.Marshal turns a Go value into XML bytes.

package main

import (
	"encoding/xml"
	"fmt"
)

type User struct {
	Name string
	Age  int
}

func main() {
	b, _ := xml.Marshal(User{"Ann", 30})
	fmt.Println(string(b))
}

XML Tags

The xml tag controls element names, just like json tags control JSON keys.

package main

import (
	"encoding/xml"
	"fmt"
)

type User struct {
	Name string "xml:\"name\""
}

func main() {
	b, _ := xml.Marshal(User{"Ann"})
	fmt.Println(string(b))
}

Root Element with XMLName

A field of type xml.Name named XMLName sets the root element's name.

package main

import (
	"encoding/xml"
	"fmt"
)

type User struct {
	XMLName xml.Name "xml:\"user\""
	Name    string   "xml:\"name\""
}

func main() {
	b, _ := xml.Marshal(User{Name: "Ann"})
	fmt.Println(string(b))
}

Attributes

Add ,attr to a tag to render a field as an XML attribute instead of a child element.

package main

import (
	"encoding/xml"
	"fmt"
)

type User struct {
	XMLName xml.Name "xml:\"user\""
	ID      int      "xml:\"id,attr\""
	Name    string   "xml:\"name\""
}

func main() {
	b, _ := xml.Marshal(User{ID: 7, Name: "Ann"})
	fmt.Println(string(b))
}

Unmarshal XML

xml.Unmarshal parses XML bytes into a pointer, matching element names to tags.

package main

import (
	"encoding/xml"
	"fmt"
)

type User struct {
	Name string "xml:\"name\""
}

func main() {
	var u User
	xml.Unmarshal([]byte("<User><name>Bob</name></User>"), &u)
	fmt.Println(u.Name)
}

Nested Elements

Nested structs map to nested XML elements, building a tree.

package main

import (
	"encoding/xml"
	"fmt"
)

type Address struct {
	City string "xml:\"city\""
}
type User struct {
	Name    string  "xml:\"name\""
	Address Address "xml:\"address\""
}

func main() {
	b, _ := xml.Marshal(User{"Ann", Address{"Rome"}})
	fmt.Println(string(b))
}

Pretty XML

xml.MarshalIndent formats output with indentation for readability.

package main

import (
	"encoding/xml"
	"fmt"
)

type User struct {
	Name string "xml:\"name\""
}

func main() {
	b, _ := xml.MarshalIndent(User{"Ann"}, "", "  ")
	fmt.Println(string(b))
}

Repeated Elements

A slice field becomes a series of repeated XML elements, one per item.

package main

import (
	"encoding/xml"
	"fmt"
)

type List struct {
	Items []string "xml:\"item\""
}

func main() {
	b, _ := xml.Marshal(List{Items: []string{"a", "b"}})
	fmt.Println(string(b))
}

JSON vs XML

The APIs mirror each other: Marshal, Unmarshal, MarshalIndent, struct tags. XML adds attributes and the XMLName root concept. Prefer JSON for new APIs; XML for legacy.

Header Declaration

The constant xml.Header holds the standard XML declaration string you can prepend to output for complete documents.

package main

import (
	"encoding/xml"
	"fmt"
)

func main() {
	fmt.Print(xml.Header)
}

Quick Check

What does the ,attr option do in an xml struct tag?

Recap

encoding/xml:

  • xml.Marshal / Unmarshal mirror the json API
  • xml tags name elements; ,attr makes attributes
  • XMLName sets the root; slices repeat elements

Frequently asked questions

Is the “encoding/xml” lesson free?

Yes — the full text of “encoding/xml” is free to read here on the web, and the Go Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Go Academy course, upgrade to CoddyKit PRO.

What will I learn in “encoding/xml”?

Work with XML. You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Go Academy?

No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “encoding/xml” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Go Academy lesson?

Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. encoding/json Recap
  2. encoding/xml
  3. encoding/csv
  4. gob and Binary Encoding
← Back to Go Academy