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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀