@@ -260,6 +260,21 @@ it as well, and call its Run method.
260260To upload, first define a type that implements the ValueSaver interface, which has a single method named Save.
261261Then create an Inserter, and call its Put method with a slice of values.
262262
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+
263278 u := table.Inserter()
264279 // Item implements the ValueSaver interface.
265280 items := []*Item{
@@ -272,15 +287,33 @@ Then create an Inserter, and call its Put method with a slice of values.
272287 }
273288
274289You 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:
277309
278310 type Item2 struct {
279311 Name string
280312 Size float64
281313 Count int
282314 }
283- // Item implements the ValueSaver interface.
315+
316+ // Item2 doesn't implement ValueSaver interface, so schema will be inferred.
284317 items2 := []*Item2{
285318 {Name: "n1", Size: 32.6, Count: 7},
286319 {Name: "n2", Size: 4, Count: 2},
0 commit comments