@@ -260,6 +260,21 @@ it as well, and call its Run method.
260
260
To upload, first define a type that implements the ValueSaver interface, which has a single method named Save.
261
261
Then create an Inserter, and call its Put method with a slice of values.
262
262
263
+ type Item struct {
264
+ Name string
265
+ Size float64
266
+ Count int
267
+ }
268
+
269
+ // Save implements the ValueSaver interface.
270
+ func (i *Item) Save() (map[string]bigquery.Value, string, error) {
271
+ return map[string]bigquery.Value{
272
+ "Name": i.Name,
273
+ "Size": i.Size,
274
+ "Count": i.Count,
275
+ }, "", nil
276
+ }
277
+
263
278
u := table.Inserter()
264
279
// Item implements the ValueSaver interface.
265
280
items := []*Item{
@@ -272,15 +287,33 @@ Then create an Inserter, and call its Put method with a slice of values.
272
287
}
273
288
274
289
You can also upload a struct that doesn't implement ValueSaver. Use the StructSaver type
275
- to specify the schema and insert ID by hand, or just supply the struct or struct pointer
276
- directly and the schema will be inferred:
290
+ to specify the schema and insert ID by hand:
291
+
292
+ type item struct {
293
+ Name string
294
+ Num int
295
+ }
296
+
297
+ // Assume schema holds the table's schema.
298
+ savers := []*bigquery.StructSaver{
299
+ {Struct: score{Name: "n1", Num: 12}, Schema: schema, InsertID: "id1"},
300
+ {Struct: score{Name: "n2", Num: 31}, Schema: schema, InsertID: "id2"},
301
+ {Struct: score{Name: "n3", Num: 7}, Schema: schema, InsertID: "id3"},
302
+ }
303
+
304
+ if err := u.Put(ctx, savers); err != nil {
305
+ // TODO: Handle error.
306
+ }
307
+
308
+ Lastly, but not least, you can just supply the struct or struct pointer directly and the schema will be inferred:
277
309
278
310
type Item2 struct {
279
311
Name string
280
312
Size float64
281
313
Count int
282
314
}
283
- // Item implements the ValueSaver interface.
315
+
316
+ // Item2 doesn't implement ValueSaver interface, so schema will be inferred.
284
317
items2 := []*Item2{
285
318
{Name: "n1", Size: 32.6, Count: 7},
286
319
{Name: "n2", Size: 4, Count: 2},
0 commit comments