title: Encode array to base64 in go
body: <p>Here is my complete code for a function I develop:</p>
  
  <pre><code>package main
  
  import (
      "database/sql"
      "log"
      "encoding/xml"
      "github.com/gin-gonic/gin" //golang frameworks
      _ "github.com/go-sql-driver/mysql"
      "gopkg.in/gorp.v1"        //work with database(mysql, etc.)
  )
  
  type Genre struct {
      Title string `xml:"genre"`
  }
  
  type GenreArray struct {
      Auth_state int `xml:"auth_state"`
      Count int64 `xml:"count"`
      Item genreAPI `xml:"item"`
  }
  
  type UserPass struct {
      Username string `xml:"username"`
      Password string `xml:"password"`
  }
  
  type ErrorMessage struct {
      XMLName xml.Name `xml:"error"`
      Error string `xml:"error_code"`
      Message string `xml:"message"`
  }
  
  type Auth struct {
      XMLName xml.Name `xml:"config"`
      Nas_SharingEmpty  int `xml:"nas_sharing&gt;auth_state"`
  }
  
  type ConfigGenre struct {
      XMLName xml.Name `xml:"config"`
      Nas_Sharing  GenreArray `xml:"nas_sharing"`
  }
  
  type genreAPI []Genre
  
  var dbmap = initDb()
  
  func initDb() *gorp.DbMap {
  
      db, err := sql.Open("mysql", "root@tcp(localhost:3306)/mymusic")
      checkErr(err, "sql.Open failed")
      dbmap := &amp;gorp.DbMap{Db: db, Dialect: gorp.MySQLDialect{"InnoDB", "UTF8"}}
  
      return dbmap
  }
  
  func checkErr(err error, msg string) {
      if err != nil {
          log.Fatalln(msg, err)
      }
  }
  
  func main() {
      r := gin.Default()
      r.POST("/nas_sharing", myMusicXML)
      r.Run(":9999")
  }
  
  func myMusicXML(c *gin.Context) {
  
      c.Request.ParseForm()
      cmd := c.Request.Form.Get("cmd")
      usernamepost := c.Request.Form.Get("user")
      passwordpost := c.Request.Form.Get("passwd")
  
      if cmd == "36" {
      //Music Genre List API
  
          var genre []Genre
          var userpass []UserPass
          var count int64
          var countAuth int64
  
          //Query the post username and password
          _, errAuth := dbmap.Select(&amp;userpass, "SELECT username, password FROM user WHERE username=? and password=?", usernamepost, passwordpost)
  
          //Check if post username and password exist
          countAuth, errAuth = dbmap.SelectInt("select count(*) FROM user WHERE username=? and password=?", usernamepost, passwordpost)
          if countAuth == 0 {
              //If no rows detected, output a message
              c.XML(404, Auth{Nas_SharingEmpty: 0})
          }else{
              //Check if query is valid
              if errAuth == nil {
                  log.Println("auth_state :", countAuth)
              }else{
                  c.XML(404, gin.H{"error": "sql query error found"})
              }
  
              //Query genre list
              _, err := dbmap.Select(&amp;genre, "SELECT Title FROM genre_cntr_tbl")
  
              //Count genres
              count, err = dbmap.SelectInt("select count(*) FROM genre_cntr_tbl")
              if count == 0 {
                  //If no rows detected, output a message
                  c.XML(404, ErrorMessage{Error:"404", Message: "no genre found"})
              }else{
                  //Check if query is valid
                  if err == nil {
                      log.Println("Genres :", genre)
                      c.XML(200, ConfigGenre{Nas_Sharing: GenreArray{Auth_state: 1, Count: count, Item: genre,}})
                  }else{
                      c.XML(404, gin.H{"error": "sql query error found"})
                  }
              }
          }
      }else{
          c.XML(404, ErrorMessage{Error:"404", Message: "command not found"})
      }
  }
  </code></pre>
  
  <p>And here is the output:</p>
  
  <pre><code>&lt;config&gt;
  &lt;nas_sharing&gt;
      &lt;auth_state&gt;1&lt;/auth_state&gt;
      &lt;count&gt;8&lt;/count&gt;
      &lt;item&gt;
          &lt;genre&gt;Pop&lt;/genre&gt;
      &lt;/item&gt;
      &lt;item&gt;
          &lt;genre&gt;Rock&lt;/genre&gt;
      &lt;/item&gt;
      &lt;item&gt;
          &lt;genre&gt;Dance&lt;/genre&gt;
      &lt;/item&gt;
      &lt;item&gt;
          &lt;genre&gt;Opera&lt;/genre&gt;
      &lt;/item&gt;
      &lt;item&gt;
          &lt;genre&gt;Techno&lt;/genre&gt;
      &lt;/item&gt;
      &lt;item&gt;
          &lt;genre&gt;Hip Hop&lt;/genre&gt;
      &lt;/item&gt;
      &lt;item&gt;
          &lt;genre&gt;Jazz&lt;/genre&gt;
      &lt;/item&gt;
      &lt;item&gt;
          &lt;genre&gt;Reggae&lt;/genre&gt;
      &lt;/item&gt;
  &lt;/nas_sharing&gt;
  </code></pre>
  
  <p></p>
  
  <p>Here is the value of genre of my output to log(note: It came from a database):</p>
  
  <pre><code>Genres : [{Pop} {Rock} {Dance} {Opera} {Techno} {Hiphop} {Jazz} {Reggae}]  
  </code></pre>
  
  <p>However, I would like to convert the output to base64 format. Here's a sample code for encoding to base64 but the data given to this is <code>string</code> unlike the one I develop that comes from an array. How could I achieve that output?</p>
  
  <pre><code>data := "/MyMusic/images/_albums/albums_nophoto.png"
  sEnc := b64.StdEncoding.EncodeToString([]byte(data))
  fmt.Println(sEnc)
  </code></pre>
  
  <p>Here is what output I expected</p>
  
  <pre><code>&lt;config&gt;
  &lt;nas_sharing&gt;
      &lt;auth_state&gt;1&lt;/auth_state&gt;
      &lt;count&gt;8&lt;/count&gt;
      &lt;item&gt;
          &lt;genre&gt;UG9w&lt;/genre&gt;
      &lt;/item&gt;
      &lt;item&gt;
          &lt;genre&gt;Um9jaw==&lt;/genre&gt;
      &lt;/item&gt;
      &lt;item&gt;
          &lt;genre&gt;RGFuY2U=&lt;/genre&gt;
      &lt;/item&gt;
      &lt;item&gt;
          &lt;genre&gt;T3BlcmE=&lt;/genre&gt;
      &lt;/item&gt;
      &lt;item&gt;
          &lt;genre&gt;VGVjaG5v&lt;/genre&gt;
      &lt;/item&gt;
      etc...
  </code></pre>