128 points olexsmir 2 hours ago 35 comments
lolpanda 1 hour ago | parent
hnlmorg 1 hour ago | parent
Plus the point of the marshal interface is that it’s reusable.
thayne 1 hour ago | parent
hnlmorg 1 hour ago | parent
If you wanted type safety with JSON in Go then don’t use the marshal interface. Period.
thayne 50 minutes ago | parent
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 37 minutes ago | parent
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 1 hour ago | parent
NoDodgeQuestion 1 hour ago | parent
iaaan 1 hour ago | parent
sgsjchs 1 hour ago | parent
lolpanda 1 hour ago | parent
duskwuff 57 minutes ago | parent
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.
bicarbonato 21 minutes ago | parent
Because they are common types, makes sense to precautionary have the error return.
CamouflagedKiwi 1 hour ago | parent
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.
athorax 1 hour ago | parent
v := jsontext.Value(b)
v.Indent()
fmt.Println(string(v))irq-1 19 minutes ago | parent
func WithIndent(indent string) Options
https://pkg.go.dev/encoding/json/jsontext#WithIndentarccy 1 hour ago | parent
json.Marshal(v, jsontext.WithIndent(" "))
?modulovalue 1 hour ago | parent
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.
lightandlight 52 minutes ago | parent
weitendorf 36 minutes ago | parent
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
asim 1 hour ago | parent
So yea hopefully a nice new addition but only my prompts now matter. So need to make sure they pickup on the new API.
dofm 1 hour ago | parent
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 19 minutes ago | parent
dofm 3 minutes ago | parent
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.
citizenpaul 16 minutes ago | parent
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?
dofm 1 hour ago | parent
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 49 minutes ago | parent
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 38 minutes ago | parent
JSON processing, quite markedly, is renown in Go for being slow, so I don't really understand why it took so long
hnlmorg 27 minutes ago | parent
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 13 minutes ago | parent
Only about a year from when the Haskell guys lent their expertise 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.
troupo 40 minutes ago | parent
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.
wiseowise 33 minutes ago | parent