来自 fs.Stats 类的 Node.js stats.gid 属性
stats.gid 属性是 fs.Stats 类的内置应用程序编程接口,用于获取文件所属组的数字(数字/大整数)标识。
句法:
stats.gid;
返回值:它返回一个数字或 BigInt 值,表示拥有该文件的组的身份。
以下示例说明了 Node.js 中 stats.gid 属性的使用:
示例 1:
// Node.js program to demonstrate the
// stats.gid property
// Accessing fs module
const fs = require('fs');
// Calling fs.Stats stats.gid
// for directory using stat
fs.stat('./', (err, stats) => {
if (err) throw err;
console.log("using stat: numeric "
+ "identity of the group is "
+ stats.gid);
});
// Using lstat
fs.lstat('./', (err, stats) => {
if (err) throw err;
console.log("using lstat: numeric "
+ "identity of the group is "
+ stats.gid);
});
// For file
// Using stat
fs.stat('./filename.txt', (err, stats) => {
if (err) throw err;
console.log("using stat: numeric "
+ "identity of the group is "
+ stats.gid);
});
// Using lstat
fs.lstat('./filename.txt', (err, stats) => {
if (err) throw err;
console.log("using lstat: numeric "
+ "identity of the group is "
+ stats.gid);
});
输出:
using stat: numeric identity of the group is 5687
using lstat: numeric identity of the group is 5687
using stat: numeric identity of the group is 5687
using lstat: numeric identity of the group is 5687
示例 2:
// Node.js program to demonstrate the
// stats.gid property
// Accessing fs module
const fs = require('fs').promises;
// Calling fs.Stats stats.gid
(async () => {
const stats = await fs.stat('./filename.txt');
console.log("using stat synchronous: numeric"
+ " identity of the group is " + stats.gid);
})().catch(console.error)
输出:
(node:15204) ExperimentalWarning: The fs.promises API
is experimental
using stat synchronous: numeric identity of the group is 5687
注意:以上程序将通过使用node filename.js
命令编译运行,并正确使用 file_path。此 API 将在 POSIX 系统中正常工作。在 WINDOWS 等其他系统中,它将返回 0。
参考: https://nodejs.org/api/fs.html#fs_stats_gid