Skip to content
Node: fs.writeFile(path, data)github.com/sahilkhaire/gox/fs

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) error

Compare: 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)

Back to fs package overview

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