diff --git a/go/arrow/schema.go b/go/arrow/schema.go index 1936cea989f..f95c3336522 100644 --- a/go/arrow/schema.go +++ b/go/arrow/schema.go @@ -197,6 +197,16 @@ func (sc *Schema) FieldIndices(n string) []int { return sc.index[n] } +// FieldIndex return index of the unique field with the given name. +// Returns -1 if the name isn’t found or there are several fields with the given name. +func (sc *Schema) FieldIndex(name string) int { + indices := sc.index[name] + if len(indices) != 1 { + return -1 + } + return indices[0] +} + func (sc *Schema) HasField(n string) bool { return len(sc.FieldIndices(n)) > 0 } func (sc *Schema) HasMetadata() bool { return len(sc.meta.keys) > 0 } diff --git a/go/arrow/schema_test.go b/go/arrow/schema_test.go index af9576003e1..63929570ccd 100644 --- a/go/arrow/schema_test.go +++ b/go/arrow/schema_test.go @@ -292,6 +292,15 @@ func TestSchema(t *testing.T) { } } + if s.FieldIndex("dup") != -1 { + t.Fatalf("invalid FieldIndex(dup): got=%d, want=-1", s.FieldIndex("dup")) + } + if s.HasField("f1") { + if s.FieldIndex("f1") != 0 { + t.Fatalf("invalid FieldIndex(f1): got=%d, want=0", s.FieldIndex("f1")) + } + } + if got, want := s.String(), tc.serialize; got != want { t.Fatalf("invalid stringer: got=%q, want=%q", got, want) }