tree: 9160be81683ab9cc26f04325b2b5dff44cb2e8ed [path history] [tgz]
  1. templates/
  2. BUILD.gn
  3. golang.go
  4. README.md
zircon/tools/zither/backends/golang/README.md

The Zither Go Backend

The Zither Go backend is gives Go data layout bindings.

Output layout

One file ${filename}.go is generated per original FIDL source file, containing the bindings for the declarations defined there. Given a FIDL library by the name of id1.id2.....idn, these files comprise a package of name fidl/data/${id1}/.../${idn}.

Additionally, a pkg_name.txt is generated with the package name for its contents.

GN integration

${fidl_target}_zither.golang gives a go_library() defining the generated package.

Bindings

Any declaration type not mentioned below is ignored.

Built-in types

FIDL typeGo type
int8int8
int16int16
int32int32
int64int64
uint8uint8
uint16uint16
uint32uint32
uint64uint64
boolbool
stringstring
ucharbyte
usize64uint
uintptr64uintptr
array<T, N>[N]T'

Note that FIDL strings are only permitted as constants.

Constants

const INT_CONST uint32 = 10;  // Or any integral type.
const STR_CONST string = "string constant";

yields

const IntConst uint32  = 10
const StringConst string = "string constant"

Enums

type MyEnum = enum : int8 {  // Or any valid integral type
    ZERO = 0;
    ONE = 1;
};

yields

type MyEnum int8

const (
    MyEnumZero MyEnum = 0
    MyEnumOne MyEnum = 1
)

Bits

type MyBits = bits : uint8 {  // Or any valid integral type
    ONE = 1;
    TWO = 2;
    FOUR = 4;
};

yields

type MyBits uint8

const (
    MyBitsOne  MyBits = 1 << 0
    MyBitsTwo  MyBits = 1 << 1
    MyBitsFour MyBits = 1 << 2
)

Structs

type MyStruct = struct {
    member_a uint64;
    member_b bool;
};

yields

type MyStruct struct {
   MemberA uint64
   MemberB bool
}

Aliases

alias MyAlias = MyType;

yields

type MyAlias = MyType

Overlays

type MyOverlay = strict overlay {
    1: a MyOverlayStructVariant;
    2: b uint32;
};

yields

type MyOverlayDiscriminant uint64

const (
	MyOverlayDiscriminantA MyOverlayDiscriminant = 1
	MyOverlayDiscriminantB MyOverlayDiscriminant = 2
)

type MyOverlay struct {
	Discriminant MyOverlayDiscriminant
	variant      [8]byte
}

func (o MyOverlay) IsA() bool {
	return o.Discriminant == MyOverlayDiscriminantA
}

func (o *MyOverlay) AsA() *MyOverlayStructVariant {
	if !o.IsA() {
		return nil
	}
	return (*MyOverlayStructVariant)(unsafe.Pointer(&o.variant))
}

func (o MyOverlay) IsB() bool {
	return o.Discriminant == MyOverlayDiscriminantB
}

func (o *MyOverlay) AsB() *uint32 {
	if !o.IsB() {
		return nil
	}
	return (*uint32)(unsafe.Pointer(&o.variant))
}