drrvnbwle80177811 2019-07-18 07:23
浏览 197
已采纳

从exec.Command写入文件

I am trying to write a file from a bash command into a file in Go. Note there are several reasons for using Go over bash here: I have some more logic such as parsing configuration files, I would like to run that code for multiple DBs in parallele and finally performing some more complex data manipulation after.

    dumpStr := fmt.Sprintf("pg_dump -U %s -h %s %s | gzip", DbUserName, DbHost, DbName)
    cmd := exec.Command("bash", "-c", dumpStr)
    cmd.Env = append(cmd.Env, "PGPASSWORD="+DbPassword)

    outfile, err := os.Create(DbName + ".gz")
    if err != nil {
        panic(err)
    }
    outfile = cmd.Stdout
    defer outfile.Close()
    err = cmd.Start()
    if err != nil {
        panic(err)
    }
    cmd.Wait()

However, I am getting an emtpy result. I am getting data if I am executing dumpStr from the CLI but not from that code... What am I missing?

  • 写回答

2条回答 默认 最新

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 如何修改为正确的?求解决
      • ¥15 django访问管理员界面问题
      • ¥20 python homework完成
      • ¥20 求解 多变量系统的最小二乘辨识问题的推导以及matlab仿真
      • ¥15 arduino esp8266 Blinker编译报问题
      • ¥15 ubuntu18.04运行模型,直接死机
      • ¥30 (问卷调查)莫名其妙丢了u盘,你们会是什么心理状态
      • ¥100 Spark+android应⽤案例
      • ¥15 yolov8 目标检测 重叠 遮挡
      • ¥20 微信聊天记录如何部署到服务器上