TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Go Protobuf: The New Opaque API

287 pointsby secure5 months ago

21 comments

dpeckett5 months ago
To be honest I kind of find myself drifting away from gRPC&#x2F;protobuf in my recent projects. I love the idea of an IDL for describing APIs and a great compiler&#x2F;codegen (protoc) but there&#x27;s just soo many idiosyncrasies baked into gRPC at this point that it often doesn&#x27;t feel worth it IMO.<p>Been increasingly using LSP style JSON-RPC 2.0, sure it&#x27;s got it&#x27;s quirks and is far from the most wire&#x2F;marshaling efficient approach but JSON codecs are ubiquitous and JSON-RPC is trivial to implement. In-fact I recently even wrote a stack allocated, server implementation for microcontrollers in Rust <a href="https:&#x2F;&#x2F;github.com&#x2F;OpenPSG&#x2F;embedded-jsonrpc">https:&#x2F;&#x2F;github.com&#x2F;OpenPSG&#x2F;embedded-jsonrpc</a>.<p>Varlink (<a href="https:&#x2F;&#x2F;varlink.org&#x2F;" rel="nofollow">https:&#x2F;&#x2F;varlink.org&#x2F;</a>) is another interesting approach, there&#x27;s reasons why they didn&#x27;t implement the full JSON-RPC spec but their IDL is pretty interesting.
评论 #42436618 未加载
评论 #42438439 未加载
评论 #42437594 未加载
评论 #42436623 未加载
评论 #42436517 未加载
评论 #42438122 未加载
评论 #42436724 未加载
评论 #42436278 未加载
评论 #42440147 未加载
kyrra5 months ago
The opaque API brings some niceties that other languages have, specifically about initialization. The Java impl for protobuf will never generate a NullPointerException, as calling `get` on a field would just return the default instance of that field.<p>The Go OpenAPI did not do this. For many primative types, it was fine. But for protobuf maps, you had to check if the map had been initialized yet in Go code before accessing it. Meaning, with the Opaque API, you can start just adding items to a proto map in Go code without thinking about initialization. (as the Opaque impl will init the map for you).<p>This is honestly something I wish Go itself would do. Allowing for nil maps in Go is such a footgun.
评论 #42435707 未加载
评论 #42439632 未加载
评论 #42439127 未加载
parhamn5 months ago
It&#x27;s interesting, to everyone but but the mega shops like Google, protobuf is a schema declaration tool. To the megashops its a performance tool.<p>For most of my projects, I use a web-framework I built on protobuf over the years but slowly got rid of a lot of the protobufy bits (besides the type + method declarations) and just switched to JSON as the wire format. http2, trailing headers, gigantic multi-MB files of getters, setters and embedded binary representations of the schemas, weird import behaviors, no wire error types, etc were too annoying.<p>Almost every project I&#x27;ve tracked that tries to solve the declarative schema problem seems to slowly die. Its a tough problem an opinionated one (what to do with enums? sum types? defaults? etc). Anyone know of any good ones that are chugging along? OpenAPI is too resty and JSONSchema doesn&#x27;t seem to care about RPC.
评论 #42439113 未加载
评论 #42441017 未加载
评论 #42437852 未加载
评论 #42440111 未加载
jeffbee5 months ago
Protobuf 3 was bending over backwards to try to make the Go API make sense, but in the process it screwed up the API for C++, with many compromises. Then they changed course and made presence explicit again in proto 3.1. Now they are saying Go gets a C++-like API.<p>What I&#x27;d like is to rewind the time machine and undo all the path-dependent brain damage.
评论 #42435599 未加载
评论 #42435760 未加载
评论 #42436011 未加载
评论 #42436127 未加载
评论 #42439161 未加载
评论 #42438335 未加载
评论 #42435586 未加载
the_gipsy5 months ago
&gt; syntax = &quot;proto2&quot; uses explicit presence by default<p>&gt; syntax = &quot;proto3&quot; used implicit presence by default (where cases 2 and 3 cannot be distinguished and are both represented by an empty string), but was later extended to allow opting into explicit presence with the optional keyword<p>&gt; edition = &quot;2023&quot;, the successor to both proto2 and proto3, uses explicit presence by default<p>The root of the problem seems to be go&#x27;s zero-values. It&#x27;s like putting makeup on a pig, your get rid of null-panics, but the null-ish values are still everywhere, you just have bad data creeping into every last corner of your code. There is no amount of validation that can fix the lack of decoding errors. And it&#x27;s not runtime errors instead of compile-time errors, which can be kept in check with unit tests to some degree. It&#x27;s just bad data and defaulting to carry on no matter what, like PHP back in the day.
评论 #42441361 未加载
评论 #42439834 未加载
评论 #42440541 未加载
评论 #42441735 未加载
评论 #42442324 未加载
kubb5 months ago
I hate this API and Go&#x27;s handling of protocol buffers in general. Especially preparing test data for it makes for some of the most cumbersome and unwieldy files that you will ever come across. Combined with table driven testing you have thousands upon thousands of lines of data with an unbelievably long identifiers that can&#x27;t be inferred (e.g. in array literals) that is usually copy pasted around and slightly changed. Updating and understanding all of that is a nightmare and if you miss a coma or a brace somewhere, the compiler isn&#x27;t smart enough to point you to where so you get lines upon lines of syntax errors. But, being opaque has some advantages for sure.
评论 #42436467 未加载
评论 #42442650 未加载
评论 #42437631 未加载
评论 #42438720 未加载
评论 #42435594 未加载
remram5 months ago
&gt; version: 2, 3, 2023 (released in 2024)<p>I call this Battlefield versioning, after the Battlefield video game series [1]. I bet the next version will be proto V.<p>[1]: in order: 1942, 2, 2142, 3, 4, 1, V, 2042
评论 #42449493 未加载
matrix875 months ago
I recently used code-gen&#x27;d protobuf deser objects as the value type for an in-memory db and was considering flattening them into a more memory-efficient representation and using bitfields. That was for java though, not sure if they are doing the same thing there<p>Glad to see this change, for that use case it would&#x27;ve been perfect
评论 #42438614 未加载
h4ch15 months ago
Surprisingly I saw this on the front page mere minutes after deciding to use protobufs in my new project.<p>Currently I&#x27;m not quite sold on RPC since the performance benefits seem to show up on a much larger scale than what I am aiming for, so I&#x27;m using a proto schema to define my types and using protoc codegen to generate only JSON marshaling&#x2F;unmarshaling + types for my golang backed and typescript frontend, with JSON transferred between the two using REST endpoints.<p>Seems to give me good typesafety along with 0 headache in serializing&#x2F;deserializing after transport.<p>One thing I also wanted to do was generate SQL schemas from my proto definitions or SQL migrations but haven&#x27;t found a tool to do so yet, might end up making one.<p>Would love to know if any HN folk have ideas&#x2F;critique regarding this approach.
favflam5 months ago
Oh, this is great. I just did an implementation in gRPC in Go whereby I had to churn through 10MB&#x2F;s of data. I could not implement any kind of memory pool and thus I had a lot of memory allocation issues which lead to bad memory usage and garbage collection eating up my CPU.
评论 #42440571 未加载
cyberax5 months ago
Thanks. I hate it.<p>Now you can not use normal Go struct initialization and you&#x27;ll have to write reams of Set calls.
评论 #42437556 未加载
评论 #42436735 未加载
tonymet5 months ago
why is code generation under-utilized? protobufs and other go tooling are great for code generation. Yet in practice i see few teams using it at scale.<p>Lots of teams creating rest &#x2F; json APIs, but very few who use code generation to provide compile-time protection.
评论 #42436346 未加载
评论 #42436377 未加载
alakra5 months ago
Is this like the FlatBuffers &quot;zero-copy&quot; deserialization?
评论 #42435338 未加载
评论 #42435359 未加载
neonsunset5 months ago
The absolute state of Go dragging down the entire gRPC stack with it. Oh well, at least we have quite a few competent replacements nowadays.
评论 #42445205 未加载
g0ld3nrati05 months ago
just curious, why do use protobuf instead of flatbuffers?
评论 #42438494 未加载
评论 #42438646 未加载
评论 #42437936 未加载
strawhatguy5 months ago
Great, now there&#x27;s an API per struct&#x2F;message to learn and communicate throughout the codebase, with all the getters and setters.<p>A given struct is probably faster for protobuf parsing in the new layout, but the complexity of the code probably increases, and I can see this complexity easily negating these gains.
评论 #42435341 未加载
评论 #42435654 未加载
评论 #42435316 未加载
评论 #42435378 未加载
tuetuopay5 months ago
I can’t wait to try this new Protobuf Enterprise Edition, with its sea of getters and setters ad nauseam. &#x2F;s<p>However I can get behind it for the lazy decoding which seems nice, though I doubt its actual usefulness for serious software (tm). As someone else already mentioned, an actual serious api (tm) will have business-scope types to uncouple the api definition from the implementation. And that’s how you keep sane as soon as you have to support multiple versions of the api.<p>Also, a lot of the benefits mentioned for footgun reductions smell like workarounds for the language shortcomings. Memory address comparisons, accidental pointer sharing and mutability, enums, optional handling, etc are already solved problems and where something like rust shines. (Disclaimer: I run several grpc apis written in rust in prod)
Naru415 months ago
Why not just use a naive struct from the beginning? memcpy is the fastest way to get serialize into a form that we can use in actual running program.
评论 #42438406 未加载
评论 #42438618 未加载
cyberax5 months ago
BTW, if you care so much about performance, then fix the freaking array representation. It should be simple `[]SomeStruct` instead of `[]*SomeStruct`.<p>This one small change can result in an order of magnitude improvement.
评论 #42445324 未加载
abtinf5 months ago
This looks like an attempt to turn Go into Java&#x2F;C#.<p>I certainly won’t allow this to be used by the engineering teams under me.
评论 #42440301 未加载
评论 #42438477 未加载
评论 #42438675 未加载
lakomen5 months ago
Graphql won the race for me. Grpc is no longer relevant. Too many hurdles, no proper to and from Web support. You have to use some 3rd party non free service.
评论 #42436322 未加载
评论 #42436275 未加载