Пример #1
0
func ExampleInsert() {
	// books.capnp:
	// struct Book {
	//   title @0 :Text;
	//   pageCount @1 :Int32;
	// }

	type Book struct {
		Title     string
		PageCount int32
	}

	// Allocate a new Cap'n Proto Book struct.
	_, seg, err := capnp.NewMessage(capnp.SingleSegment(nil))
	if err != nil {
		panic(err)
	}
	root, err := books.NewRootBook(seg)
	if err != nil {
		panic(err)
	}

	// Insert the book struct into the Cap'n Proto struct.
	b := &Book{
		Title:     "War and Peace",
		PageCount: 1440,
	}
	if err := pogs.Insert(books.Book_TypeID, root.Struct, b); err != nil {
		panic(err)
	}
	fmt.Println(root)

	// Output:
	// (title = "War and Peace", pageCount = 1440)
}
Пример #2
0
func writer(out io.Writer) {
	// Make a brand new empty message.  A Message allocates Cap'n Proto structs.
	msg, seg, err := capnp.NewMessage(capnp.SingleSegment(nil))
	if err != nil {
		panic(err)
	}

	// Create a new Book struct.  Every message must have a root struct.
	book, err := books.NewRootBook(seg)
	if err != nil {
		panic(err)
	}
	book.SetTitle("War and Peace")
	book.SetPageCount(1440)

	// Write the message to stdout.
	err = capnp.NewEncoder(out).Encode(msg)
	if err != nil {
		panic(err)
	}
}