doulan8846
2019-01-25 18:45 浏览 51
已采纳

执行psql命令时os / exec上的输出不完整

I'm currently using os/exec to run many psql commands in sequence.

My problem is that if there's an error during the execution of the script, the output doesn't show it.

Example: When executing the command file from the command line, the output is this:

SET
CREATE FUNCTION
psql:update-14.3.7.1/dml/----------------xxxxxxxx.sql:33: ERROR:  null value in column "tipoxxxxxxxx" violates not-null constraint
DETALHE:  Failing row contains (9, 0, null, null, null, null, AAAAAAAAA_BBBBBBBBBB_CCCCC_BANANA, null, null, null).
CONTEXTO:  SQL statement "insert INTO ----------------xxxxxxxx
        (id,versao,tipoxxxxxxxx,carrotdeapplefinal,carrotdetttttttt,ativa,funcionalidade,driver,alterar_na_xxxxxxxx)
        values
        (nextval('sq_----------------xxxxxxxx'),0,tipoaaaaaaaaa,carrotapple,carrottttttttt,alterarNaxxxxxxxx)"
PL/pgSQL function createorupdateaaaaaaaaa() line 22 at SQL statement
DROP FUNCTION

And executing the exact same file from the Go code, the output is:

SET
CREATE FUNCTION
DROP FUNCTION

My Go code for running commands(and showing the output) is:

    cmd := exec.Command(comando, argList...)
    var out bytes.Buffer
    var stderr bytes.Buffer
    cmd.Stdout = &out
    cmd.Stderr = &stderr
    err := cmd.Run()
    cmdS := out.String()
    fmt.Println("cmdS", cmdS)
    if err != nil {
        fmt.Println("XYZ")
        fmt.Println(fmt.Sprint(err) + ": " + stderr.String())
    }

How to modify my code to show the other lines of the output?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答 默认 最新

  • 已采纳
    dongshuo9350 dongshuo9350 2019-01-25 19:26

    Programs can use stderr and stdout however they choose. The presence of an execution error has no bearing on whether there was something written to stderr or not.

    Always check both streams when looking for all output from a program.

    点赞 评论 复制链接分享

相关推荐