A new Go API for JSON

(go.dev)

136 points | by olexsmir 3 hours ago

9 comments

  • asim 1 hour ago
    At this stage to me Go is the new C compiler. Quite honestly I spent 10 years or more writing Go code. At times 18 hours a day. And now with AI, I get models to write my Go code. I STILL want it to be Go code. Is that strange? No because I understand the performance and safety of Go. But I rarely write it directly anymore. Probably not at all in 6-9 months. Yet I still occasionally look at the code to see what the output looks like.

    So yea hopefully a nice new addition but only my prompts now matter. So need to make sure they pickup on the new API.

    • jacobgold 2 minutes ago
      I still prefer Go over the alternatives because I'm reviewing the code carefully and trying to make it all look as good as if I'd written it very carefully by hand.

      The few times I've needed a language without a GC and used Rust and C as target languages, I've reviewed the code less and enjoyed it less.

    • biztos 6 minutes ago
      As I do more agentic coding, I am starting to feel strongly that I need to set aside a project -- and time -- for strictly hand-coding, using AI (if at all) only to explain things, never to write any code.

      First, I like programming and don't want to stop doing it just because a machine can do it too. Second, I'm pretty sure atrophied coding skills will make me worse at directing the machines, thus worse at what seems to be "the job" these days.

    • dofm 1 hour ago
      This is mostly why I have come back to Go.

      I am using local LLMs and Go has a bunch of strengths where smaller contexts are concerned; less complicated flow of control being one of them.

      • calvinmorrison 37 minutes ago
        not a lot of overloading complexity, very verbose with a small total language structure too helps.
        • dofm 21 minutes ago
          Yes. It's that thing — the fact that a language helps you read what a single function does really easily, but in so doing makes the entire codebase quite dense, is not optimal for us, but is good for them.

          And the things that we do to manage this, that are symbolic or based on visceral real-world analogy, are good for us and bad for them.

          Go is kind of in the middle, in the way that many vaguely Wirth-like and CSP-like languages possibly are, I guess.

    • egnehots 8 minutes ago
      Truly the argument can be made either way.

      If you code in Go, the code is more high level, prettier, simpler.

      If you code in Rust, the code is more robust, efficient, faster.

      Both are important for LLM, why can't we have a high level, simple language that is also robust, efficient and fast? =)

    • citizenpaul 34 minutes ago
      People are sleeping on Go as a LLM language of choice. Everyone trying to rewrite in rust which is fine and rust is fine. However its compile times are several orders of magnitude greater than Go. You can have LLMs write and compile 20 apps in the time one rust app compiles. I've got a Go personal project that would take hours for rust to compile, I can't imagine making the same velocity in Rust. Though I do see the workplace appeal of bringing back the xkcd "compiling" meme.

      However it kind of makes me feel like my professional life was wasted and I don't really know what kind of future I have. One of Go's main points was you can pick it up in a weekend. What career moat do people like us have?

      • pjmlp 9 minutes ago
        I would rather have LLM directly generate machine code, and best of all, macro Assemblers are blazing fast for decades.
  • modulovalue 1 hour ago
    I'm working on better SIMD support in Dart and one of the motivations for that work is specializing parser specifications based on schema specifications of that language.

    Or more concretely, if we had a parser generator + json parser specification that can generate efficient simdjson style parsers, then we should be able to parse json restricted to a subset of json based on a schema at least as efficiently as simdjson can parse general purpose json.

    That way we could have the best of both worlds, a (potentially lazy) statically type-safe model and simdjson performance!

    I still think that this is definitely doable, it's just that a lot of infrastructure needs to be built for it.

    I remember reading that Meta is doing something similar with binary formats. I find it weird that there's nothing like that for json yet.

    • weitendorf 55 minutes ago
      I was working on metaparsing earlier this year (specifically over grpc because of its better support for types and better serialization format).

      I think most people who use JSON don't want strong typing or to use schemas. If you do, you just use a different serialization format: JSON is designed to be a generic, human-readable way to basically represent any kind of data structure (since it deserializes to JS objects). So everything about JSON is working against you if you do try to enforce type/schemas on it.

      A grpc-like protocol with more prescriptive type registries/discovery (over the network) and better interop with http (like grpc-gateway) would be the best approach IMO. Definitely not JSON schema

    • lightandlight 1 hour ago
  • CamouflagedKiwi 2 hours ago
    I generally like `encoding/json/v2` and I'm glad they did it.

    I wish they'd left `MarshalIndent` in there though. The equivalent code is super complicated and non-obvious, so it's a pain to migrate, and it's a pain when you're banging out some quick code and want to dump some readable JSON. I suppose these days the LLMs can write the complicated version for us, of course.

  • wiseowise 52 minutes ago
    This is how you roll out features, C++. Congrats to Go team and users.
  • dofm 1 hour ago
    I… think this is good?

    I'm just getting the neuron-band back together where Golang is concerned. I have done some in the past for little system tasks, but I am switching away from interpreted languages entirely for web stuff, and initially to Go, as I switch away from hand-coding to at least LLM-supported coding.

    So I am a bit out of date on the pain points (I never really felt pain points when I last used it, because the jobs I was doing had very small, quite Go-friendly shapes).

  • cbarrick 1 hour ago
    The fact that this took 6 years to land stood out to me.

    I'm not entirely surprised, and I'm glad the Go team is carefully considering their interfaces before launching. But 6 years seems like a lot of work to land a JSON API.

    • awesome_dude 56 minutes ago
      Sometimes I wonder about Go's velocity - generics (something that I still haven't used beyond the tutorials) took an age as well

      JSON processing, quite markedly, is renown in Go for being slow, so I don't really understand why it took so long

      • hnlmorg 46 minutes ago
        I think in the case of JSON, it was “good enough”. People who found this to be a problem would use one of the many 3rd party options.

        I kind of like that the standard library doesn’t move fast. It makes it easier to fit the ecosystem into my head (unlike, for example, the JavaScript ecosystem where I find code doesn’t survive 2 years before failing to compile).

        But I do agree that slow and steady produces its own problems. Your example of generics is a good one. But think the example of Go modules is another, perhaps even better, example of how sometimes the Go team really should have accelerated their efforts on.

      • win311fwg 32 minutes ago
        > generics (something that I still haven't used beyond the tutorials) took an age as well

        Only about a year from when the Haskell guys lent their expertise in designing it and it landing in a release. That's actually fairly quick given that it was a pretty significant change under the hood and constrained by only two releases annually.

        The Go team was always clear that they didn't have the necessary capabilities internally, despite a lot of failed efforts trying anyway, and were relying on the open source community to lend a hand. Unfortunately, open source doesn't mean open willingness to contribute. It turns out that for all the experts on HN, posting "go doesn't have generics lol" was preferable to helping out.

        • enneff 9 minutes ago
          Haskell guys? Got a reference to this? That’s news to me.
  • troupo 58 minutes ago
    > in any, out any

    Completely unrelated: can anyone tell me why language authors introduce in/out/inout function parameters (see also C#).

    It feels like this is some rather bad workaround for something that breaks assumptions about parameters being passed into the function.

    • duskwuff 9 minutes ago
      "in" and "out" are the names of the parameters, not qualifiers.
  • birchcove 37 minutes ago
    [flagged]
  • lolpanda 2 hours ago
    the marshal api has been very annoying. why does marshal return an error? the only reason the marshal can return an error is that the input type is not marshalable. it's certainly a code bug. to me it's like int+str returning an error instead of compiler error
    • duskwuff 1 hour ago
      > why does marshal return an error? the only reason the marshal can return an error is that the input type is not marshalable

      Not necessarily. Among other things, MarshalJSON methods called during marshaling can return an error for whatever reason the developer sees fit - one possibility that comes to mind is that part of the object tree being marshalled is lazily loaded, and failed to do so.

      And, as others have mentioned, there are types like "any" whose marshalability can't be determined at compile time. Those types aren't necessarily at the top level, either; they could appear anywhere in the object tree.

    • sgsjchs 1 hour ago
      Types may be mostly marshallabble, have a subset of values that are unmarshallable. E.g. non-finite floats.
    • bicarbonato 39 minutes ago
      AFAIK it's mostly because of floats. JSON doesn't support `NaN` and `infinity` for example.

      Because they are common types, makes sense to precautionary have the error return.

    • 0x696C6961 1 hour ago
      Then write yourself a MustMarshal wrapper that panics.
      • hnlmorg 44 minutes ago
        The GP is talking about compile time errors. You’re discussing exceptions, which is still a runtime check
      • jeremyloy_wt 1 hour ago
        A generic Must[T] you mean ;)
    • hnlmorg 2 hours ago
      I wouldn’t class unmarshallable data as a compile time error.

      Plus the point of the marshal interface is that it’s reusable.

      • thayne 1 hour ago
        It could be if go had a more powerful type system.
        • hnlmorg 1 hour ago
          This isn’t a type system problem. It’s a data origin problem. Ie it could be any random JSON blob a user entered. The compiler doesn’t know this ahead of time.

          If you wanted type safety with JSON in Go then don’t use the marshal interface. Period.

          • thayne 1 hour ago
            For unmarshalling, but the original complaint was about marshalling. And for that the type system could help if the marshal API only allowed you to marshal objects of types that supported marshalling.

            And even for unmarshalling you could get a compile time error if you try to unmarshall to a type that doesn't support it.

            • hnlmorg 55 minutes ago
              I’d already addressed that when I said the marshal API is an interface that’s meant to be reusable across generalise use cases. it can be used for a variety of data formats and not just JSON.

              Go has this concept baked into the language. For example read and write interfaces so you can write data to an endpoint without having to worry about whether it’s a file system object, TCP/UDP socket, gzip achieve, and a plethora of other options too.

              It’s a nice convenience feature and makes up considerably for the restrictions that the Go type system places.

              And if you need higher performance code, or compile time safety, or have any other constraints that limit your ability to use these generalised interfaces, then you can use different APIs from the Go standard library to achieve the same thing.

              You don’t need to call json.Marshal() if you don’t want to. It’s an option, but it’s just one API into the JSON toolbox.

    • leetrout 2 hours ago
      JSON is a runtime input of unknown type but in your example those are known types so the compiler can catch them.
    • lolpanda 1 hour ago
      i meant i hoped it's panic as opposed to return an error. if there was an error, how am i supposed to handle it? can i tell the user hey sorry my bad it's likely a code error? crash is better than possible eating the error siliently
    • NoDodgeQuestion 1 hour ago
      do not know at compile time that input is not marshalable, every language marshal must return error for this reason, not only golang
    • iaaan 1 hour ago
      It is trivial to ignore an error in Go if you know your use case cannot result in one. But at the end of the day, json.Marshal takes an `any` as a parameter, so it must return an error.