ParseFile
Overview
ParseFile reads path and unmarshals into v.
Part of the json package — Node.js analog: JSON.parse/stringify.
Signature
go
func ParseFile(ctx context.Context, path string, v any) errorCompare: Node.js · Standard Go · gox
js
JSON.parse(fs.readFileSync('cfg.json','utf8'));go
b, err := os.ReadFile(path)
err = json.Unmarshal(b, &cfg)go
import "github.com/sahilkhaire/gox/json"
dir := t.TempDir()
path := filepath.Join(dir, "data.json")
var m map[string]intExample
go
import "github.com/sahilkhaire/gox/json"
dir := t.TempDir()
path := filepath.Join(dir, "data.json")
var m map[string]intTips
Import github.com/sahilkhaire/gox/json and call ParseFile directly. See the comparison below for the standard library equivalent.
Standard library alternative
Use the standard library directly:
go
b, err := os.ReadFile(path)
err = json.Unmarshal(b, &cfg)