Skip to content

feat: Add fixed-size array support to the Go code generator#8974

Open
statxc wants to merge 5 commits intogoogle:masterfrom
statxc:go/fixed-size-array-support
Open

feat: Add fixed-size array support to the Go code generator#8974
statxc wants to merge 5 commits intogoogle:masterfrom
statxc:go/fixed-size-array-support

Conversation

@statxc
Copy link
Contributor

@statxc statxc commented Mar 11, 2026

Summary

Add fixed-size array support to the Go code generator, resolving a long-standing gap where Go was the only major language without support for the [type:length] struct field syntax (fixes #8417).

Changes

  • src/idl_parser.cpp: Add IDLOptions::kGo to the SupportsAdvancedArrayFeatures() allowlist.
  • src/idl_gen_go.cpp: Implement array-aware code generation for Go, including:
    • Indexed accessors and length methods for scalar and struct array fields
    • Scalar array mutators with bounds-checked index access
    • Builder body logic with correct reverse-order writing and nested struct flattening
    • Object API (Pack/UnPack) support using Go fixed-size arrays ([N]Type)
  • tests/go_arrays_test_test.go: Comprehensive test covering read, mutate, object API round-trip, and standalone struct creation against the arrays_test.fbs schema.
  • tests/GoArraysTest.sh: Test runner script following the same pattern as GoTest.sh.

Design

The implementation follows the same flattening strategy used by the Java and C# generators: nested struct array fields are expanded into per-sub-field slices in the Create function signature. For example, d:[NestedStruct:2] becomes d_a [][]int32, d_b []TestEnum, d_c [][]TestEnum, d_d [][]int64. Field offsets were verified against the Java reference output.

Test plan

  • go vet clean on generated code
  • All 4 new tests pass (TestArrayStructRead, TestArrayStructMutate, TestArrayStructObjectAPI, TestNestedStructStandalone)
  • Existing Go tests (GoTest.sh) still pass
  • flatc builds with no warnings

@statxc statxc requested a review from dbaileychess as a code owner March 11, 2026 17:38
@github-actions github-actions bot added golang c++ codegen Involving generating code from schema labels Mar 11, 2026
@statxc
Copy link
Contributor Author

statxc commented Mar 11, 2026

@jtdavis777 would you review this PR
thank you

@jtdavis777
Copy link
Collaborator

this is also a language I am not familiar with, and that I think we do not have an active maintainer of. so will take me some time

@statxc statxc force-pushed the go/fixed-size-array-support branch from 5a05954 to b9564b4 Compare March 11, 2026 18:16
@github-actions github-actions bot added the rust label Mar 11, 2026
@statxc
Copy link
Contributor Author

statxc commented Mar 17, 2026

@jtdavis777 could you give me any updates for me?

@jtdavis777
Copy link
Collaborator

this looks like another chunky feature that will take me a little bit to digest -- what I can say is there is a SupportsAdvancedArrayFeatures feature in the idl_parser I think you will need to enable -- it isn't a well documented area of the code but we will need to make sure Go conforms to all the features under this umbrella.

@statxc
Copy link
Contributor Author

statxc commented Mar 19, 2026

this looks like another chunky feature that will take me a little bit to digest -- what I can say is there is a SupportsAdvancedArrayFeatures feature in the idl_parser I think you will need to enable -- it isn't a well documented area of the code but we will need to make sure Go conforms to all the features under this umbrella.

Good call on SupportsAdvancedArrayFeatures - that's already enabled in this PR(kGo added to the whitelist in idl_parser.cpp).
I checked Go's coverage against the other backends. Read, mutate, create, and full object API round-trip all work and are tested against arrays_test.fbs. Two small gaps vs C++/Rust:

  1. No dedicated struct array mutator - though it still works because D(nil, 0) returns a pointer into the buffer, so you can mutate fields on the returned struct directly.
  2. No full-array slice getter (same as TypeScript).

Happy to close either gap if you want full parity.

@jtdavis777
Copy link
Collaborator

thanks for the explanation -- I didn't catch in my quick scan of your PR that you had already enabled. I don't think absolute parity is necessary out of the gate as long as basic functionality is present. I will do my best to review this (with some of my free time next week most likely) but I would also love to see if I can ping someone else familiar with flatbuffers Go to chime in

@jtdavis777
Copy link
Collaborator

@mustiikhalil do you have any Go experience? :)

@jtdavis777
Copy link
Collaborator

I am pretty sure there are some example FBS files that ensure that flatc can generate code for them and at least one of them is a dedicated arrays test. can you poke around and see if we can make sure Go is one of the languages tested with that?

@statxc
Copy link
Contributor Author

statxc commented Mar 20, 2026

@jtdavis777 Good catch. I looked into it and there are two things I need to hook up:

  1. scripts/generate_code.py - this is the central script that generates code from arrays_test.fbs for C++, C#, Java, Rust, Python, and Swift. Go is already included there for monster_test.fbs but not for arrays_test.fbs. I'll add --go to the arrays_test generation call.
  2. tests/TestAll.sh - GoArraysTest.sh exists but isn't called here. I'll add it next to GoTest.sh.

I'll get both of those wired up.

@statxc statxc force-pushed the go/fixed-size-array-support branch from 33c56b7 to c05dbb2 Compare March 20, 2026 12:15
@statxc
Copy link
Contributor Author

statxc commented Mar 20, 2026

@jtdavis777 Good catch. I looked into it and there are two things I need to hook up:

  1. scripts/generate_code.py - this is the central script that generates code from arrays_test.fbs for C++, C#, Java, Rust, Python, and Swift. Go is already included there for monster_test.fbs but not for arrays_test.fbs. I'll add --go to the arrays_test generation call.
  2. tests/TestAll.sh - GoArraysTest.sh exists but isn't called here. I'll add it next to GoTest.sh.

I'll get both of those wired up.

Updated! please review again @jtdavis777
thanks

@jtdavis777
Copy link
Collaborator

test side LGTM - would still love to find another golang person to look this over

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c++ codegen Involving generating code from schema golang python rust

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Go] Fixed Size Array

2 participants