Getting values from Deno stdin

后端 未结 5 1087
梦毁少年i
梦毁少年i 2021-02-14 11:43

How can we get values from standard input in Deno?

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

An example would be appreciated.

5条回答
  •  你的背包
    2021-02-14 11:50

    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)));
      }
    }
    

提交回复
热议问题