How to get diskusage in golang?

3,301 views
Skip to first unread message

jianfeng ye

unread,
Aug 19, 2014, 4:39:46 AM8/19/14
to golan...@googlegroups.com
Now I want to get the disk usage in golang.

I know I can got that by call command "df ", but I want to know is there any other method to get the diskusage status?


Andrew Gerrand

unread,
Aug 19, 2014, 5:02:39 AM8/19/14
to jianfeng ye, golang-nuts


On 19 August 2014 18:39, jianfeng ye <jianfe...@gmail.com> wrote:
Now I want to get the disk usage in golang.

I know I can got that by call command "df ", but I want to know is there any other method to get the diskusage status?


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

j4pe

unread,
Aug 19, 2014, 5:06:16 AM8/19/14
to golan...@googlegroups.com
Hello,

In this thred https://groups.google.com/forum/#!topic/golang-nuts/Ld41Pdk28tY Han-Wen Nienhuys propose :

s := syscall.Statfs_t{}
errNo := syscall.Statfs(ts.orig, &s)

http://golang.org/pkg/syscall/#Statfs
http://golang.org/pkg/syscall/#Statfs_t

regards

Andrew Gerrand

unread,
Aug 19, 2014, 5:14:11 AM8/19/14
to golang-nuts
Incidentally, that's exactly what the library I suggested does under the covers:



Konstantin Khomoutov

unread,
Aug 19, 2014, 5:16:21 AM8/19/14
to jianfeng ye, golan...@googlegroups.com
On a POSIX system, you can os.Stat("/") and calculate the disk usage
from the values provided in the system-specific fields of the
FileInfo value. This will be syscall.Stat_t.
See man stat(2) for more info.

On Windows, you should possibly call GetDiskFreeSpace() API function.

1. http://golang.org/pkg/syscall/#Stat_t
2. http://msdn.microsoft.com/en-us/library/windows/desktop/aa364935

jianfeng ye

unread,
Aug 19, 2014, 2:31:20 PM8/19/14
to golan...@googlegroups.com, jianfe...@gmail.com
Thanks, I got that, But further, it has a param call "path", I want to show all the disks status like "df"

Filesystem     1K-blocks     Used Available Use% Mounted on

/dev/xvda1      20641404 14142760   5450120  73% /

tmpfs             960368        0    960368   0% /dev/shm

/dev/xvdb1      51599192   184268  48793832   1% /alidata1

what should i do?

在 2014年8月19日星期二UTC+8下午5时02分39秒,Andrew Gerrand写道:

DV

unread,
Aug 19, 2014, 3:08:06 PM8/19/14
to golan...@googlegroups.com, jianfe...@gmail.com
On *nix, you can do http://linux.die.net/man/2/statvfs, on Windows - http://msdn.microsoft.com/en-us/library/windows/desktop/aa364937(v=vs.85).aspx
Pick one (or both) and write code that uses the syscall package to call one (or the other, or both). 

Or, you can cheap out and call "df" from a shell process you start in Go, and parse its output...

have fun!

jianfeng ye

unread,
Aug 19, 2014, 3:08:10 PM8/19/14
to golan...@googlegroups.com, jianfe...@gmail.com
That means I want to know how to get all potitions in golang?

在 2014年8月20日星期三UTC+8上午2时31分20秒,jianfeng ye写道:

Will Norris

unread,
Aug 20, 2014, 10:54:33 AM8/20/14
to jianfeng ye, golang-nuts
if this SO question is any indication, it doesn't appear that there is a system call for listing mount points.  Depending on how portable you need this code to be, reading from /proc/mounts may be your best bet.

Roger Pack

unread,
Aug 21, 2014, 4:35:23 PM8/21/14
to golan...@googlegroups.com


On Tuesday, August 19, 2014 3:06:16 AM UTC-6, Jean-Alexandre Peyroux wrote:
Hello,

In this thred https://groups.google.com/forum/#!topic/golang-nuts/Ld41Pdk28tY Han-Wen Nienhuys propose :

s := syscall.Statfs_t{}
errNo := syscall.Statfs(ts.orig, &s)

http://golang.org/pkg/syscall/#Statfs
http://golang.org/pkg/syscall/#Statfs_t

Does this work in windows? There's no cross platform way to do this using the std lib, I guess?

Andrew Gerrand

unread,
Aug 21, 2014, 9:25:37 PM8/21/14
to Roger Pack, golang-nuts

On 22 August 2014 06:35, Roger Pack <rogerp...@gmail.com> wrote:
Does this work in windows? There's no cross platform way to do this using the std lib, I guess?

No, it will not. And no, there is not.

StalkR

unread,
Aug 22, 2014, 2:07:18 AM8/22/14
to Roger Pack, golang-nuts
See also https://groups.google.com/d/msg/golang-dev/0Xl2ULeg6oQ/afIQ7xqkATgJ
tldr: proposal to add Statfs to windows syscall package as first step towards a cross-platform way of doing this, it was based on GetDiskFreeSpace API (as used by https://github.com/StalkR/goircbot/blob/master/lib/disk/space_windows.go) but it was thought unnecessary for stdlib and preferred to be in an external library.

--

Roger Pack

unread,
Aug 22, 2014, 7:06:47 AM8/22/14
to StalkR, golang-nuts
On Fri, Aug 22, 2014 at 12:06 AM, StalkR <sta...@stalkr.net> wrote:
> See also https://groups.google.com/d/msg/golang-dev/0Xl2ULeg6oQ/afIQ7xqkATgJ
> tldr: proposal to add Statfs to windows syscall package as first step
> towards a cross-platform way of doing this, it was based on GetDiskFreeSpace
> API (as used by
> https://github.com/StalkR/goircbot/blob/master/lib/disk/space_windows.go)
> but it was thought unnecessary for stdlib and preferred to be in an external
> library.

Makes sense to me, you would have gotten my +1 :)
-roger-
Reply all
Reply to author
Forward
0 new messages