'How to get a list of system users in vlang?
I am trying to list all the system users, I know that on Linux I could use
awk -F: '{ print $1 }' /etc/passwd
and on Windows I could use wmic useraccount get name
but I was wondering if there was function in V that would be better suited for it and didn't require different commands for Linux and Windows.
Solution 1:[1]
EDIT: You can now use fn user_names() ?[]string
from the os
module in order to get the names of users on the system.
There currently is now way to get a list of users on the system. However you can you compile time if statements to execute the commands you referred to above.
import os
fn main() {
$if windows {
result := os.execute('wmic useraccount get name')?
} $else $if linux {
result := os.execute('awk -F: "{ print $1 }" /etc/passwd')?
} $else {
println('unsupported os')
}
// do something with result
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |