在這個(gè)程序中,每個(gè)命令行參數(shù)都是一個(gè)文件名,參數(shù)對(duì)應(yīng)的文件將被依次打開,打印到標(biāo)準(zhǔn)輸出流。
const filenames = Deno.args;
for (const filename of filenames) {
const file = await Deno.open(filename);
await Deno.copy(file, Deno.stdout);
file.close();
}
除了內(nèi)核到用戶空間再到內(nèi)核的必要拷貝,這里的 copy() 函數(shù)不會(huì)產(chǎn)生額外的昂貴操作,從文件中讀到的數(shù)據(jù)會(huì)原樣寫入標(biāo)準(zhǔn)輸出流。這反映了 Deno I/O 流的通用設(shè)計(jì)目標(biāo)。 嘗試一下:
deno run --allow-read https://deno.land/std/examples/cat.ts /etc/passwd
更多建議: