How do I print message to stderr in Go?

前端 未结 3 797
臣服心动
臣服心动 2021-01-04 06:50

I want to print some logs for debugging and testing, but existing logs are massive, so I want to print my own logs to stderr:

go run main.go 1>/dev/null
<         


        
3条回答
  •  时光说笑
    2021-01-04 07:19

    There are multiple methods to send a message to stderr:

    1. Creating a new log.Logger:

      l := log.New(os.Stderr, "", 1)
      l.Println("log message")
      
    2. Using fmt.Fprintf:

      fmt.Fprintf(os.Stderr, "log message: %s", str)
      
    3. Directly writing to os.Stderr using os.Stderr.WriteString:

      os.Stderr.WriteString("log message")
      

提交回复
热议问题