Skip to content
Node: JSON.parse/stringifygithub.com/sahilkhaire/gox/json

WriteFile

Overview

WriteFile marshals v and writes to path (helper for examples).

Part of the json package — Node.js analog: JSON.parse/stringify.

Signature

go
func WriteFile(ctx context.Context, path string, v any, perm os.FileMode) error

Compare: Node.js · Standard Go · gox

js
fs.writeFileSync('out.json', JSON.stringify(obj));
go
b, err := json.Marshal(obj)
err = os.WriteFile(path, b, 0644)
go
import "github.com/sahilkhaire/gox/json"

err := json.WriteFile(ctx, "out.json", obj)

Example

go
import "github.com/sahilkhaire/gox/json"

err := json.WriteFile(ctx, "out.json", obj)

Tips

Import github.com/sahilkhaire/gox/json and call WriteFile directly. See the comparison below for the standard library equivalent.

Standard library alternative

Use the standard library directly:

go
b, err := json.Marshal(obj)
err = os.WriteFile(path, b, 0644)

Back to json package overview

MIT Licensed · Built for Node.js developers moving to Go