Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8559579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:05:08+00:00 2026-06-11T16:05:08+00:00

In Go, you can marshall a struct to XML, e.g.: package main import (

  • 0

In Go, you can marshall a struct to XML, e.g.:

package main

import (
    "encoding/xml"
    "fmt"
    )

type person struct {
    Name string
    Starsign string
}

func main() {
    p := &person{"John Smith", "Capricorn"}
    b,_ := xml.MarshalIndent(p,"","   ")
    fmt.Println(string(b))
}

produces output:

<person>
   <Name>John Smith</Name>
   <Starsign>Capricorn</Starsign>
</person>

My problem is, the person type is lower-case “p” because I want that to be private to the package. But I’d prefer the XML element to be uppercase: <Person>. The fields within the struct can be marshalled to other names using tags (e.g. `xml:”name”`) against the structure fields but this doesn’t seem to be an option for the structure type.

I have a work-around using templates, but it would be nice to know a better answer.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T16:05:10+00:00Added an answer on June 11, 2026 at 4:05 pm

    According to the encoding/xml.Marshal documentation:

    The name for the XML elements is taken from, in order of preference:

    • the tag on the XMLName field, if the data is a struct
    • the value of the XMLName field of type xml.Name
    • the tag of the struct field used to obtain the data
    • the name of the struct field used to obtain the data
    • the name of the marshalled type

    You can use a tag on the XMLName field in the struct to override the person struct’s XML tag name. In order to avoid putting it in your actual person struct, you can create an anonymous struct that embeds the person struct you are marshaling.

    package main
    
    import (
        "encoding/xml"
        "fmt"
    )
    
    type person struct {
        Name        string
        Starsign    string
    }
    
    func marshalPerson(p person) ([]byte, error) {
        tmp := struct {
            person
            XMLName struct{}    `xml:"Person"`
        }{person: p}
    
        return xml.MarshalIndent(tmp, "", "   ")
    }
    
    func main() {
        p := person{"John Smith", "Capricorn"}
        b, _ := marshalPerson(p)
        fmt.Println(string(b))
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a recommendation for a pythonic library that can marshall python objects to
In WinForms, you have Control.BeginInvoke() , which means you can marshall a call from
Why can't I use sizeof() on simple structs? eg: private struct FloatShortPair { public
i need to know how can i resolve Unions and Type** (e.g. int**) with
Given a struct MyStruct , I can get the size of instances of that
Using .NET 4.0 I can quickly convert a struct to and from an array
I have Spring REST application that can marshall an object to JSON when a
Am trying to marshall a class that contains a List of Interface type. JAXB
I need to marshall and unmarshall a Java class to XML. The class in
Inspired by How can I marshal a hash with arrays? I wonder what's the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.