dongpao9165 2019-07-25 00:03
浏览 85
已采纳

如果可以将Println设置为变量,如何显示Println的错误值?

It says:

Printf formats according to a format specifier and writes to standard output. It returns the number of bytes written and any write error encountered.

How do I create/test an error and show the error if it is stored in b when I run the program it shows "nil" since no error, how can i show any error?

  a,b :=fmt.Println("Hello, playground")
    fmt.Println(a)
    fmt.Println(b)

https://golang.org/pkg/fmt/#Println
func Println(a ...interface{}) (n int, err error)
https://play.golang.org/p/8Cjb2Sfunx7
  • 写回答

1条回答 默认 最新

  • doutao6380 2019-07-25 02:26
    关注

    It is unusual to have Println errors, the docs also this. But if you are really interesting in doing a test of it, it works:

    package main
    
    import (
        "fmt"
        "os"
        "log"
    )
    
    func main() {
        const name, age = "Kim", 22
        os.Stdout.Close()
        _, err := fmt.Println(name, "is", age, "years old.")
        log.Fatal(err)
    
    }
    

    In order to artificially trigger an error we are closing the default Stdout file, used by Println. See the os documentation.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab实现基于主成分变换的图像融合。
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊