codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🐹

Go (Golang)

32 / 72 topics
27Standard Library Overview28IO Package29Net Package30HTTP Package31JSON Package32XML Package33Time Package34OS Package35Fmt Package36Math Package37Regexp Package38Log Package39Flag Package40Context Package41Embed Package
Tutorials/Go (Golang)/XML Package
🐹Go (Golang)

XML Package

Updated 2026-04-20
3 min read

XML Package

The encoding/xml package in Go provides functionality for encoding and decoding XML data. This is particularly useful when working with web services, configuration files, or any other format that uses XML. In this tutorial, we will explore how to use the encoding/xml package to encode and decode XML data.

Overview

XML (eXtensible Markup Language) is a markup language that defines a set of rules for encoding documents in a format that is both human-readable and machine-readable. The encoding/xml package allows Go programs to easily convert between XML and Go data structures.

Key Features

  • Encoding: Convert Go data structures into XML.
  • Decoding: Parse XML data into Go data structures.
  • Customization: Support for custom tags, namespaces, and more.

Getting Started

To use the encoding/xml package, you need to import it in your Go program:

import (
    "encoding/xml"
    "fmt"
)

Encoding XML

Encoding involves converting a Go data structure into an XML string. Let's start with a simple example.

Example 1: Basic Encoding

Suppose we have a struct representing a person:

type Person struct {
    Name string `xml:"name"`
    Age  int    `xml:"age"`
}

We can encode this struct to XML as follows:

func main() {
    p := Person{Name: "John Doe", Age: 30}

    // Encode the person struct to XML
    xmlData, err := xml.MarshalIndent(p, "", "    ")
    if err != nil {
        fmt.Println("Error encoding XML:", err)
        return
    }

    fmt.Printf("Encoded XML:\n%s\n", xmlData)
}

Explanation

  • xml.MarshalIndent: This function encodes the given data structure into an XML string. The second and third parameters are used to format the output with indentation.
  • Tags: The struct tags (e.g., xml:"name") specify how each field should be represented in the XML.

Example 2: Complex Encoding

Let's consider a more complex example with nested structures:

type Address struct {
    City string `xml:"city"`
    Zip  string `xml:"zip,attr"`
}

type Person struct {
    Name    string  `xml:"name"`
    Age     int     `xml:"age"`
    Address Address `xml:"address"`
}

Encoding this structure:

func main() {
    p := Person{
        Name: "John Doe",
        Age:  30,
        Address: Address{
            City: "New York",
            Zip:  "10001",
        },
    }

    xmlData, err := xml.MarshalIndent(p, "", "    ")
    if err != nil {
        fmt.Println("Error encoding XML:", err)
        return
    }

    fmt.Printf("Encoded XML:\n%s\n", xmlData)
}

Explanation

  • Nested Structures: The Address struct is nested within the Person struct. The resulting XML will reflect this nesting.
  • Attributes: The Zip field in the Address struct uses ,attr to specify that it should be encoded as an attribute.

Decoding XML

Decoding involves parsing an XML string and converting it into a Go data structure.

Example 1: Basic Decoding

Suppose we have the following XML:

<person>
    <name>John Doe</name>
    <age>30</age>
</person>

We can decode this XML into a Person struct:

func main() {
    xmlData := []byte(`
        <person>
            <name>John Doe</name>
            <age>30</age>
        </person>
    `)

    var p Person
    err := xml.Unmarshal(xmlData, &p)
    if err != nil {
        fmt.Println("Error decoding XML:", err)
        return
    }

    fmt.Printf("Decoded Person: %+v\n", p)
}

Explanation

  • xml.Unmarshal: This function parses the XML data and populates the provided struct with the parsed values.

Example 2: Complex Decoding

Let's decode the more complex XML from the previous encoding example:

func main() {
    xmlData := []byte(`
        <person>
            <name>John Doe</name>
            <age>30</age>
            <address city="New York" zip="10001"/>
        </person>
    `)

    var p Person
    err := xml.Unmarshal(xmlData, &p)
    if err != nil {
        fmt.Println("Error decoding XML:", err)
        return
    }

    fmt.Printf("Decoded Person: %+v\n", p)
}

Explanation

  • Attributes: The Zip field in the Address struct is decoded from an attribute.

Best Practices

  1. Use Struct Tags Wisely: Ensure that your struct tags match the XML structure you are working with.
  2. Handle Errors: Always check for errors when encoding or decoding XML to handle any issues gracefully.
  3. Indentation: Use xml.MarshalIndent for pretty-printing XML, especially during development and debugging.

Advanced Topics

Namespaces

XML namespaces can be handled using the xml package by specifying the namespace in the struct tags.

type Person struct {
    XMLName xml.Name `xml:"http://example.com/person person"`
    Name    string   `xml:"name"`
    Age     int      `xml:"age"`
}

Custom Encoders and Decoders

You can implement custom encoding and decoding logic by implementing the MarshalXML and UnmarshalXML methods for your structs.

func (p Person) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
    // Custom encoding logic
    return e.EncodeElement(p, start)
}

func (p *Person) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
    // Custom decoding logic
    var v struct {
        XMLName xml.Name `xml:"person"`
        Name    string   `xml:"name"`
        Age     int      `xml:"age"`
    }
    if err := d.DecodeElement(&v, &start); err != nil {
        return err
    }
    *p = Person{Name: v.Name, Age: v.Age}
    return nil
}

Conclusion

The encoding/xml package in Go provides powerful tools for working with XML data. Whether you are encoding Go data structures to XML or decoding XML into Go structs, this package offers flexibility and control over the process. By following best practices and leveraging advanced features like namespaces and custom encoders/decoders, you can effectively manage XML data in your Go applications.

This tutorial has covered the basics of encoding and decoding XML using the encoding/xml package, along with some advanced topics. With this knowledge, you should be able to handle most XML-related tasks in your Go projects.


PreviousJSON PackageNext Time Package

Recommended Gear

JSON PackageTime Package