JPoz  
     

One Reason You Should Learn Go

Jul 9 2013

If you're still on the fence about Go. Here's something you should know. XML parsing in Go is awesome.

Did you just let out a viseral grown when you read XML. Then Go is for you. I hate XML but I work on web tools so I've got to deal with it.

Real life example

S3 returns XML. Really really ugly XML. But Go makes it super easy to deal with. Checkout this XML from S3:



  super-awesome-bucket
  users/186
  
  1000
  false
  
    users/186/taco.txt
    2013-07-09T22:01:47.000Z
    "8ba99404a19a173e3d964b981201061f"
    4305
  
  
    users/186/waffle.txt
    2013-07-09T22:01:41.000Z
    "8ba99404a19a173e3d964b981201061f"
    4305
  

I'm listing all the assets for user 186. All I really care about are the Keys inside of the Contents elements. Lets write some Go todo this:

package main

import "fmt"
import "encoding/xml"

var payload = "" // above

type BucketItem struct {
    Key string
}

type ListBucketResult struct {
    Contents []BucketItem
}

func main() {
    result = &ListBucketResult{}

    err = xml.Unmarshal([]byte(payload), result)
    if err != nil {
        return
    }
  
  fmt.Printf("Keys: %v\n", result.Contents)
}

BAM!!! DONE.