Getting values from Deno stdin

佐手、 提交于 2020-01-16 03:57:36

问题


My question is really short, How can we get values from standard input ?

I don't know how to use Deno.stdin ?

An example would be appreciated.

Thanks,


回答1:


Deno.stdin is of type File, thus you can read from it by providing a Uint8Array as buffer and call Deno.stdin.read(buf)

window.onload = async function main() {
  const buf = new Uint8Array(1024);
  /* Reading into `buf` from start.
   * buf.subarray(0, n) is the read result.
   * If n is instead Deno.EOF, then it means that stdin is closed.
   */
  const n = await Deno.stdin.read(buf); 
  if (n == Deno.EOF) {
    console.log("Standard input closed")
  } else {
    console.log("READ:", new TextDecoder().decode(buf.subarray(0, n)));
  }
}


来源:https://stackoverflow.com/questions/58019572/getting-values-from-deno-stdin

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!