Go VsCode Editing experience 2023

Shortfalls I noticed whilst cooking up a Go program to consume some JSON endpoints

Published: Friday, May 12, 2023 Last modified: Wednesday, Oct 2, 2024

Whilst coding up a utility to piece together some JSON API responses I ran into a couple of issues:

JSON to struct

  1. How do you convert JSON to Go Structs without https://mholt.github.io/json-to-go/ locally in the editor?
  2. Perhaps you need to select part of the struct as a standalone type, moving it out in vscode with its json tags is quite tedious

Curl to struct

The boiler plate needed to convert curl commands to go is tedious and copilot is of no help.

https://mholt.github.io/curl-to-go/ helps, though it misses out on resp.StatusCode checks.

err = json.NewDecoder(resp.Body).Decode(&pc)

Furthermore Copilot doesn’t seem to be smart enough to use json.NewDecoder instead of json.Unmarshal

General JSON pain

json: cannot unmarshal number into Go struct field

Errors like the above make me think twice about using Golang to parse JSON. It’s a total pain to implement a custom UnmarshalJSON method or do typecasting!