duanke6249 2016-08-04 16:54
浏览 480
已采纳

Golang如何实现stdin / stdout / stderr

I did a little program which was able to parse input from command line. It worked well by means of std.in. However, when I looked up the official document for further learning, I found there was too much stuff for me.

var (
   Stdin = NewFile(uintptr(syscall.Stdin), "/dev/stdin")
)

I read the document of func NewFile, type uintpty, Package syscall individually but could not figure out the whole. Also, I did not know the meaning of /dev/stdin, either.

I never learned another static programming language except for go. How could I realize the magic of stdin?

  • 写回答

1条回答 默认 最新

  • doupaoyan6083 2016-08-04 17:02
    关注

    From the syscall package, Stdin is just the number 0:

    var (
            Stdin  = 0
            Stdout = 1
            Stderr = 2
    )
    

    This is simply because the posix standard is that stdin is attached to the first file descriptor, 0.

    Since stdin is always present and open by default, os.NewFile can just turn this file descriptor into an os.File, and uses the standard Linux filepath "/dev/stdin" as an easily recognizable file name.

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

报告相同问题?

悬赏问题

  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64