title: Go: How would you "Pretty Print"/"Prettify" HTML?
body: <p>In Python, PHP, and many other languages, it is possible to convert a html document and "prettify" it. In Go, this is very easily done for JSON and XML (from a struct/interface) using the MarshIndent function.</p>
  
  <p>Example for XML in Go:</p>
  
  <p><a href="http://play.golang.org/p/aBNfNxTEG1" rel="noreferrer">http://play.golang.org/p/aBNfNxTEG1</a></p>
  
  <pre><code>package main
  
  import (
      "encoding/xml"
      "fmt"
      "os"
  )
  
  func main() {
      type Address struct {
          City, State string
      }
      type Person struct {
          XMLName   xml.Name `xml:"person"`
          Id        int      `xml:"id,attr"`
          FirstName string   `xml:"name&gt;first"`
          LastName  string   `xml:"name&gt;last"`
          Age       int      `xml:"age"`
          Height    float32  `xml:"height,omitempty"`
          Married   bool
          Address
          Comment string `xml:",comment"`
      }
  
      v := &amp;Person{Id: 13, FirstName: "John", LastName: "Doe", Age: 42}
      v.Comment = " Need more details. "
      v.Address = Address{"Hanga Roa", "Easter Island"}
  
      output, err := xml.MarshalIndent(v, "  ", "    ")
      if err != nil {
          fmt.Printf("error: %v\n", err)
      }
  
      os.Stdout.Write(output)
  }
  </code></pre>
  
  <p>However, this only works for converting struct/interface into a []byte. What I want is convert a string of html code and indent automatically. Example:</p>
  
  <p>Raw HTML</p>
  
  <pre><code>&lt;!doctype html&gt;&lt;html&gt;&lt;head&gt;
  &lt;title&gt;Website Title&lt;/title&gt;
  &lt;/head&gt;&lt;body&gt;
  &lt;div class="random-class"&gt;
  &lt;h1&gt;I like pie&lt;/h1&gt;&lt;p&gt;It's true!&lt;/p&gt;&lt;/div&gt;
  &lt;/body&gt;&lt;/html&gt;
  </code></pre>
  
  <p>Prettified HTML</p>
  
  <pre><code>&lt;!doctype html&gt;
  &lt;html&gt;
      &lt;head&gt;
          &lt;title&gt;Website Title&lt;/title&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;div class="random-class"&gt;
              &lt;h1&gt;I like pie&lt;/h1&gt;
              &lt;p&gt;It's true!&lt;/p&gt;
          &lt;/div&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>How would this be done just using a string?</p>