encoding

package
v0.0.0-...-14c8dc9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 13, 2017 License: MIT Imports: 0 Imported by: 0

Documentation

Overview

Examples of encoding and decoding assorted formats.

Example (EncodingJSONComplex)
package main

import (
	"encoding/json"
	"fmt"
	"log"
)

func main() {
	type Message struct {
		Id   string
		From struct {
			Name string
			Id   string
		}
		Message string
		Actions []struct {
			Name string
			Link string
		}
		Type         string
		Created_time string
		Updated_time string
	}
	type ResponseData struct {
		Data []Message `json:"data"`
	}
	json_data := `
{
   "data": [
      {
         "id": "X999_Y999",
         "from": {
            "name": "Tom Brady", "id": "X12"
         },
         "message": "Looking forward to 2010!",
         "actions": [
            {
               "name": "Comment",
               "link": "http://www.facebook.com/X999/posts/Y999"
            },
            {
               "name": "Like",
               "link": "http://www.facebook.com/X999/posts/Y999"
            }
         ],
         "type": "status",
         "created_time": "2010-08-02T21:27:44+0000",
         "updated_time": "2010-08-02T21:27:44+0000"
      },
      {
         "id": "X998_Y998",
         "from": {
            "name": "Peyton Manning", "id": "X18"
         },
         "message": "Where's my contract?",
         "actions": [
            {
               "name": "Comment",
               "link": "http://www.facebook.com/X998/posts/Y998"
            },
            {
               "name": "Like",
               "link": "http://www.facebook.com/X998/posts/Y998"
            }
         ],
         "type": "status",
         "created_time": "2010-08-02T21:27:44+0000",
         "updated_time": "2010-08-02T21:27:44+0000"
      }
   ]
}
`
	data := ResponseData{}
	json.Unmarshal([]byte(json_data), &data)
	for _, message := range data.Data {
		fmt.Println(message)
	}
	json_dump, dump_err := json.Marshal(data)
	if dump_err != nil {
		log.Fatal(dump_err)
	}
	fmt.Println(string(json_dump))
}
Output:

{X999_Y999 {Tom Brady X12} Looking forward to 2010! [{Comment http://www.facebook.com/X999/posts/Y999} {Like http://www.facebook.com/X999/posts/Y999}] status 2010-08-02T21:27:44+0000 2010-08-02T21:27:44+0000}
{X998_Y998 {Peyton Manning X18} Where's my contract? [{Comment http://www.facebook.com/X998/posts/Y998} {Like http://www.facebook.com/X998/posts/Y998}] status 2010-08-02T21:27:44+0000 2010-08-02T21:27:44+0000}
{"data":[{"Id":"X999_Y999","From":{"Name":"Tom Brady","Id":"X12"},"Message":"Looking forward to 2010!","Actions":[{"Name":"Comment","Link":"http://www.facebook.com/X999/posts/Y999"},{"Name":"Like","Link":"http://www.facebook.com/X999/posts/Y999"}],"Type":"status","Created_time":"2010-08-02T21:27:44+0000","Updated_time":"2010-08-02T21:27:44+0000"},{"Id":"X998_Y998","From":{"Name":"Peyton Manning","Id":"X18"},"Message":"Where's my contract?","Actions":[{"Name":"Comment","Link":"http://www.facebook.com/X998/posts/Y998"},{"Name":"Like","Link":"http://www.facebook.com/X998/posts/Y998"}],"Type":"status","Created_time":"2010-08-02T21:27:44+0000","Updated_time":"2010-08-02T21:27:44+0000"}]}
Example (EncodingJSONSimple)
package main

import (
	"encoding/json"
	"fmt"
)

func main() {
	type Person struct {
		Id   int
		Name string
	}
	json_data := `{"id": 1, "name": "John Doe"}`
	fmt.Println(json_data)
	person := Person{}
	json.Unmarshal([]byte(json_data), &person)
	fmt.Printf("person.Id\n// %d\n", person.Id)
	fmt.Printf("person.Name\n// %s\n", person.Name)
}
Output:

{"id": 1, "name": "John Doe"}
person.Id
// 1
person.Name
// John Doe
Example (EncodingXMLBasic)
package main

import (
	"encoding/xml"
	"fmt"
	"log"
)

func main() {
	var xmlData = `
<?xml version="1.0" encoding="UTF-8"?>
<letter>
  <title maxlength="10"> Quote Letter </title>
  <salutation limit="40">Dear Daniel,</salutation>
  <text>Thank you f or sending us the information on <emphasis>SDL Trados Studio 2009</emphasis>.
    We like your products and think they certainly represent the most powerful translation solution on the market.
    We especially like the <component translate="yes">XML Parser rules</component> options in the <component translate="no">XML</component> filter.
    It has helped us to set up support for our XML files in a flash.
    We have already downloaded the latest version from your Customer Center.</text>
   <title maxlength="40"> Quote Details </title>
    <text> We would like to order 50 licenses.
    Please send us a quote. Keep up the good work!</text>
  
  <greetings minlength="10">Yours sincerely,</greetings>
  <signature> Paul Smith</signature>
  <address translate="yes">Smith &amp; Company Ltd.</address>
  <address translate="no">Smithtown</address>
  <weblink>http://www.smith-company-ltd.com</weblink>
  <logo alt="Logo of Smith and Company Ltd." address="http://www.smith-company-ltd.com/logo.jpg"/>
</letter>
`
	type Letter struct {
		Title      string `xml:"title"`
		Salutation string `xml:"salutation"`
	}
	var obj Letter
	err := xml.Unmarshal([]byte(xmlData), &obj)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(obj.Title[1:6])
	fmt.Println(obj.Salutation[0:4])
}
Output:

Quote
Dear
Example (EncodingXMLSlices)
package main

import (
	"encoding/xml"
	"fmt"
	"log"
)

func main() {
	var xmlData = `
<?xml version="1.0" encoding="UTF-8"?>
<letter>
  <title maxlength="10"> Quote Letter </title>
  <salutation limit="40">Dear Daniel,</salutation>
  <text>Thank you f or sending us the information on <emphasis>SDL Trados Studio 2009</emphasis>.
    We like your products and think they certainly represent the most powerful translation solution on the market.
    We especially like the <component translate="yes">XML Parser rules</component> options in the <component translate="no">XML</component> filter.
    It has helped us to set up support for our XML files in a flash.
    We have already downloaded the latest version from your Customer Center.</text>
   <title maxlength="40"> Quote Details </title>
    <text> We would like to order 50 licenses.
    Please send us a quote. Keep up the good work!</text>
  
  <greetings minlength="10">Yours sincerely,</greetings>
  <signature> Paul Smith</signature>
  <address translate="yes">Smith &amp; Company Ltd.</address>
  <address translate="no">Smithtown</address>
  <weblink>http://www.smith-company-ltd.com</weblink>
  <logo alt="Logo of Smith and Company Ltd." address="http://www.smith-company-ltd.com/logo.jpg"/>
</letter>
`
	type Letter struct {
		Text []string `xml:"text"`
	}
	var obj Letter
	err := xml.Unmarshal([]byte(xmlData), &obj)
	if err != nil {
		log.Fatal(err)
	}
	for _, v := range obj.Text {
		fmt.Println(v[1:3])
	}
}
Output:

ha
We

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL