WriteFile
Overview
WriteFile writes data to path (fs.promises.writeFile).
If you are coming from Node.js, the closest pattern is fs.writeFile(path, data).
Signature
go
func WriteFile(ctx context.Context, path string, data []byte, perm os.FileMode) errorCompare: Node.js · Standard Go · gox
js
await fs.promises.writeFile('out.txt', data);go
err := os.WriteFile("out.txt", data, 0644)go
import "github.com/sahilkhaire/gox/fs"
err := fs.WriteFile(ctx, "out.txt", data)Example
go
import "github.com/sahilkhaire/gox/fs"
err := fs.WriteFile(ctx, "out.txt", data)Tips
Pass context.Context as the first argument so cancellation and deadlines propagate correctly.
Standard library alternative
Use the standard library directly:
go
err := os.WriteFile("out.txt", data, 0644)