/
json.go
60 lines (50 loc) · 1.33 KB
/
json.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"time"
)
type Email string
type EmailMessage struct {
Subject string `json:"subject"`
Content string `json:"content"`
To Email `json:"to"`
From Email `json:"from"`
Date time.Time `json:"sent"`
}
func (e *EmailMessage) String() string {
format := "EmailMessage{\n To:%s\n From:%s\n Subject:%s\n Date:%s\n}"
return fmt.Sprintf(format, e.To, e.From, e.Subject, e.Date.String())
}
func readStream(url string) io.ReadCloser {
res, err := http.Get(url)
if err != nil || res.StatusCode != 200 {
log.Fatal("Error reading URL", err)
}
return res.Body
}
func main() {
email := &EmailMessage{
Subject: "Welcome to this message",
Content: "This email is written directly in the Go file.",
To: "hi@example.com",
From: "me@example.com",
Date: time.Now(),
}
fmt.Println("Constructed:", email)
fmt.Println("To JSON:")
encoder := json.NewEncoder(os.Stdout)
encoder.SetIndent("", " ")
encoder.Encode(email)
stream := readStream("http://echo.jsontest.com/subject/Testing" +
"/content/Hi, this is a test email/to/you@example.com/from/me@example.com" +
"/date/2018-10-09T09:43:42.19673586+01:00/")
defer stream.Close()
email = &EmailMessage{}
json.NewDecoder(stream).Decode(email)
fmt.Println("Downloaded:", email)
}