回答編集履歴

2

呼び出し方をちゃんとしました。

2019/01/28 10:40

投稿

nobonobo
nobonobo

スコア3367

test CHANGED
@@ -6,7 +6,7 @@
6
6
 
7
7
 
8
8
 
9
- [https://play.golang.org/p/mlwcYxB5BkS](https://play.golang.org/p/mlwcYxB5BkS)
9
+ [https://play.golang.org/p/xBAqGOiuE1E](https://play.golang.org/p/xBAqGOiuE1E)
10
10
 
11
11
  ```go
12
12
 
@@ -20,6 +20,8 @@
20
20
 
21
21
  "fmt"
22
22
 
23
+ "log"
24
+
23
25
  )
24
26
 
25
27
 
@@ -166,9 +168,33 @@
166
168
 
167
169
  func main() {
168
170
 
169
- fmt.Println(Get1([]byte(content)))
171
+ links, err := Get1([]byte(content))
172
+
170
-
173
+ if err != nil {
174
+
175
+ log.Fatalln(err)
176
+
177
+ }
178
+
179
+ for _, link := range links {
180
+
181
+ fmt.Println(link)
182
+
183
+ }
184
+
171
- fmt.Println(Get2([]byte(content)))
185
+ links, err = Get2([]byte(content))
186
+
187
+ if err != nil {
188
+
189
+ log.Fatalln(err)
190
+
191
+ }
192
+
193
+ for _, link := range links {
194
+
195
+ fmt.Println(link)
196
+
197
+ }
172
198
 
173
199
  }
174
200
 

1

修正

2019/01/28 10:40

投稿

nobonobo
nobonobo

スコア3367

test CHANGED
@@ -2,9 +2,11 @@
2
2
 
3
3
  個人的には後者が好みです。
4
4
 
5
+ あと、 [https://github.com/tidwall/gjson](https://github.com/tidwall/gjson) を使うという方法もあります。
5
6
 
6
7
 
8
+
7
- https://play.golang.org/p/st8U6IWLPXD
9
+ [https://play.golang.org/p/mlwcYxB5BkS](https://play.golang.org/p/mlwcYxB5BkS)
8
10
 
9
11
  ```go
10
12
 
@@ -94,11 +96,11 @@
94
96
 
95
97
 
96
98
 
97
- func Get1() ([]string, error) {
99
+ func Get1(b []byte) ([]string, error) {
98
100
 
99
101
  var v interface{}
100
102
 
101
- if err := json.Unmarshal([]byte(content), &v); err != nil {
103
+ if err := json.Unmarshal(b, &v); err != nil {
102
104
 
103
105
  return nil, err
104
106
 
@@ -138,11 +140,11 @@
138
140
 
139
141
 
140
142
 
141
- func Get2() ([]string, error) {
143
+ func Get2(b []byte) ([]string, error) {
142
144
 
143
145
  var v S
144
146
 
145
- if err := json.Unmarshal([]byte(content), &v); err != nil {
147
+ if err := json.Unmarshal(b, &v); err != nil {
146
148
 
147
149
  return nil, err
148
150
 
@@ -164,9 +166,9 @@
164
166
 
165
167
  func main() {
166
168
 
167
- fmt.Println(Get1())
169
+ fmt.Println(Get1([]byte(content)))
168
170
 
169
- fmt.Println(Get2())
171
+ fmt.Println(Get2([]byte(content)))
170
172
 
171
173
  }
172
174