teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

4

修正

2020/06/29 07:05

投稿

Gunjirk
Gunjirk

スコア23

title CHANGED
File without changes
body CHANGED
@@ -3,7 +3,8 @@
3
3
  以下のJSONを出力する
4
4
 
5
5
 
6
+ ```
6
- ```期待するJSON
7
+ //理想のJSON
7
8
  {
8
9
  "command": "ENT",
9
10
  "table": [
@@ -21,7 +22,8 @@
21
22
  }
22
23
  ```
23
24
 
25
+ ```
24
- ```現在のJSON
26
+ //現在のJSON
25
27
  {
26
28
  "command": "ENT",
27
29
  "table": [

3

不要部分の削除

2020/06/29 07:05

投稿

Gunjirk
Gunjirk

スコア23

title CHANGED
File without changes
body CHANGED
@@ -79,8 +79,8 @@
79
79
  }
80
80
 
81
81
  type Table struct {
82
- table1 Table1
82
+ Table1 Table1
83
- table2 Table2
83
+ Table2 Table2
84
84
  }
85
85
 
86
86
  func main() {
@@ -88,7 +88,12 @@
88
88
  ContentsFunc := Table2{TableName: "contents", Clientid: 0, TimeControl: 0}
89
89
  jsonStr := Command{
90
90
  Command: "ENT",
91
- Tables: []Table{BasicConfig, ContentsFunc},
91
+ Tables: []Table{
92
+ {
93
+ BasicConfig,
94
+ ContentsFunc,
95
+ },
96
+ },
92
97
  }
93
98
 
94
99
  jsonBytes, err := json.Marshal(jsonStr)
@@ -96,16 +101,16 @@
96
101
  fmt.Println("JSON Marshal error:", err)
97
102
  return
98
103
  }
104
+ out := new(bytes.Buffer)
105
+ // プリフィックスなし、スペース4つでインデント
106
+ json.Indent(out, jsonBytes, "", " ")
99
- fmt.Println(jsonBytes)
107
+ fmt.Println(out.String())
100
108
  }
101
109
 
102
110
  ```
103
111
 
104
112
 
105
113
 
106
- エラー内容
107
- .\package main.go:38:20: cannot use BasicConfig (type Table1) as type Table in slice literal
108
- .\package main.go:38:33: cannot use ContentsFunc (type Table2) as type Table in slice literal
109
114
 
110
115
 
111
116
  よろしくおねがいします。

2

追加の疑問

2020/06/29 07:04

投稿

Gunjirk
Gunjirk

スコア23

title CHANGED
File without changes
body CHANGED
@@ -3,7 +3,7 @@
3
3
  以下のJSONを出力する
4
4
 
5
5
 
6
- ```JSON
6
+ ```期待するJSON
7
7
  {
8
8
  "command": "ENT",
9
9
  "table": [
@@ -21,8 +21,33 @@
21
21
  }
22
22
  ```
23
23
 
24
+ ```現在のJSON
25
+ {
26
+ "command": "ENT",
27
+ "table": [
28
+ {
29
+ "Table1": {
30
+ "table_name": "basic",
31
+ "clientid": 0,
32
+ "server": "basic"
33
+ },
34
+ "Table2": {
35
+ "table_name": "contents",
36
+ "clientid": 0,
37
+ "time_control": 0
38
+ }
39
+ }
40
+ ]
41
+ }
42
+ ```
24
43
 
44
+ "Table1": {
45
+ "Table2": {
46
+ の部分が不要なのですが、どうしても消せません。。。
47
+
48
+
49
+
25
- エラーが起きているプログラムは以下です。
50
+ プログラムは以下です。
26
51
  異なる構造体をまとめて、リテラルスライスで出力しようとしています。
27
52
 
28
53
  ```GO

1

タイトルをわかりやすく

2020/06/29 07:01

投稿

Gunjirk
Gunjirk

スコア23

title CHANGED
@@ -1,1 +1,1 @@
1
- 構造体の継承をしたい
1
+ 構造体からJSON生成したいのですが、構造体の継承的な部分が実現できず困っています。
body CHANGED
File without changes