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 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥15 DruidDataSource一直closing
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据