Skip to content
Node: spawn(cmd, args)github.com/sahilkhaire/gox/exec

Cmd.Command

Overview

Command builds a command (child_process.spawn / execFile).

If you are coming from Node.js, the closest pattern is spawn(cmd, args).

Method on Cmd — call it on a value of that type after constructing or receiving one from a constructor.

Signature

go
func Command(ctx context.Context, name string, args ...string) *Cmd

Compare: Node.js · Standard Go · gox

js
spawn('ls', ['-la']);
go
cmd := exec.CommandContext(ctx, name, args...)
out, err := cmd.CombinedOutput()
go
import "github.com/sahilkhaire/gox/exec"

cmd := exec.Command(ctx, "ls", "-la")

Example

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

cmd := exec.Command(ctx, "ls", "-la")

Tips

Pass context.Context as the first argument so cancellation and deadlines propagate correctly.

Standard library alternative

Use the standard library directly:

go
cmd := exec.CommandContext(ctx, name, args...)
out, err := cmd.CombinedOutput()

Back to exec package overview

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