2
0

More than 3 years have passed since last update.

Goを使ってコマンドプロンプト(cmd)のコマンドを実行

Posted at

GoはPythonみたいに使えてしかも環境に合わせたバイナリを作成してくれるやばい言語

最近Goにはまっていまして単純なことをしようとしてもちょっと詰まったりしたので
備忘録としてここに残しておきます。

Goを使ってコマンドプロンプト(cmd)のコマンドを実行

以下で実行できます。あらかじめcmdを「cmd /C」しておくところがポイントです。

package main

import (
    "fmt"
    "os/exec"
    "time"
    "log"
)

func main() {
    out, err := exec.Command("cmd", "/C", "echo hello > test.txt").Output()
    if err != nil {
        log.Fatal(err)
    }else{
        fmt.Println(out)
    }
}

改訂履歴

  • 2020/8/10 新規作成
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0