@kjk1/

gobook json marshal private

Go

No description

fork
loading
Files
  • main.go

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.04924023941169464","path":"main.go","file":{"path":"main.go","content":{"asEncoding":{"base64":"cGFja2FnZSBtYWluCgppbXBvcnQgKAoJImVuY29kaW5nL2pzb24iCgkiZm10IgoJImxvZyIKKQoKLy8gOnNob3cgc3RhcnQKdHlwZSBNeVN0cnVjdCBzdHJ1Y3QgewoJdXVpZCBzdHJpbmcKCU5hbWUgc3RyaW5nCn0KCmZ1bmMgKG0gTXlTdHJ1Y3QpIE1hcnNoYWxKU09OKCkgKFtdYnl0ZSwgZXJyb3IpIHsKCWosIGVyciA6PSBqc29uLk1hcnNoYWwoc3RydWN0IHsKCQlVdWlkIHN0cmluZwoJCU5hbWUgc3RyaW5nCgl9ewoJCVV1aWQ6IG0udXVpZCwKCQlOYW1lOiBtLk5hbWUsCgl9KQoJaWYgZXJyICE9IG5pbCB7CgkJcmV0dXJuIG5pbCwgZXJyCgl9CglyZXR1cm4gaiwgbmlsCn0KCi8vIDpzaG93IGVuZAoKZnVuYyBtYWluKCkgewoJLy8gOnNob3cgc3RhcnQKCXMgOj0gTXlTdHJ1Y3R7CgkJdXVpZDogInVpZC1qb2huIiwKCQlOYW1lOiAiSm9obiIsCgl9CglkLCBlcnIgOj0ganNvbi5NYXJzaGFsKCZzKQoJaWYgZXJyICE9IG5pbCB7CgkJbG9nLkZhdGFsZigianNvbi5NYXJzaGFsSW5kZW50IGZhaWxlZCB3aXRoICclcydcbiIsIGVycikKCX0KCWZtdC5QcmludGYoIlBlcnNvbiBpbiBjb21wYWN0IEpTT046ICVzXG4iLCBzdHJpbmcoZCkpCgoJLy8gOnNob3cgZW5kCn0="},"asBuffer":null},"loaded":true}}
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
package main

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

// :show start
type MyStruct struct {
	uuid string
	Name string
}

func (m MyStruct) MarshalJSON() ([]byte, error) {
	j, err := json.Marshal(struct {
		Uuid string
		Name string
	}{
		Uuid: m.uuid,
		Name: m.Name,
	})
	if err != nil {
		return nil, err
	}
	return j, nil
}

// :show end

func main() {
	// :show start
	s := MyStruct{
		uuid: "uid-john",
		Name: "John",
	}
	d, err := json.Marshal(&s)
	if err != nil {
		log.Fatalf("json.MarshalIndent failed with '%s'\n", err)
	}
	fmt.Printf("Person in compact JSON: %s\n", string(d))

	// :show end
}