implements String() which return as JSON to every structs.

pull/4/head
Shirou WAKAYAMA 11 years ago
parent 02a91378b8
commit 8098298111

@ -26,7 +26,6 @@ Usage
::
import (
"encoding/json"
"fmt"
"github.com/shirou/gopsutil"
@ -35,12 +34,11 @@ Usage
func main() {
v, _ := gopsutil.VirtualMemory()
// return value is struct
// almost every return value is struct
fmt.Printf("Total: %v, Free:%v, UsedPercent:%f%%\n", v.Total, v.Free, v.UsedPercent)
// convert to JSON
d, _ := json.Marshal(v)
fmt.Printf("%s\n", d)
// convert to JSON. String() is also implemented
fmt.Println(d)
}
The output is below.

@ -1,6 +1,7 @@
package gopsutil
import (
"encoding/json"
"runtime"
)
@ -22,3 +23,8 @@ type CPUTimesStat struct {
func CPUCounts(logical bool) (int, error) {
return runtime.NumCPU(), nil
}
func (c CPUTimesStat) String() string {
s, _ := json.Marshal(c)
return string(s)
}

@ -1,5 +1,9 @@
package gopsutil
import (
"encoding/json"
)
type DiskUsageStat struct {
Path string `json:"path"`
Total uint64 `json:"total"`
@ -24,3 +28,18 @@ type DiskIOCountersStat struct {
WriteTime uint64 `json:"writeTime"`
Name string `json:"name"`
}
func (d DiskUsageStat) String() string {
s, _ := json.Marshal(d)
return string(s)
}
func (d DiskPartitionStat) String() string {
s, _ := json.Marshal(d)
return string(s)
}
func (d DiskIOCountersStat) String() string {
s, _ := json.Marshal(d)
return string(s)
}

@ -1,7 +1,7 @@
package gopsutil
import (
// "fmt"
"fmt"
"runtime"
"testing"
)
@ -11,12 +11,11 @@ func TestDisk_usage(t *testing.T) {
if runtime.GOOS == "windows" {
path = "C:"
}
_, err := DiskUsage(path)
v, err := DiskUsage(path)
if err != nil {
t.Errorf("error %v", err)
}
// d, _ := json.Marshal(v)
// fmt.Printf("%s\n", d)
fmt.Println(v)
}
func TestDisk_partitions(t *testing.T) {

@ -1,5 +1,9 @@
package gopsutil
import (
"encoding/json"
)
// A HostInfoStat describes the host status.
// This is not in the psutil but it useful.
type HostInfoStat struct {
@ -14,3 +18,13 @@ type UserStat struct {
Host string `json:"host"`
Started int `json:"started"`
}
func (h HostInfoStat) String() string {
s, _ := json.Marshal(h)
return string(s)
}
func (u UserStat) String() string {
s, _ := json.Marshal(u)
return string(s)
}

@ -1,7 +1,16 @@
package gopsutil
import (
"encoding/json"
)
type LoadAvgStat struct {
Load1 float64 `json:"load1"`
Load5 float64 `json:"load5"`
Load15 float64 `json:"load15"`
}
func (l LoadAvgStat) String() string {
s, _ := json.Marshal(l)
return string(s)
}

@ -1,5 +1,9 @@
package gopsutil
import (
"encoding/json"
)
type VirtualMemoryStat struct {
Total uint64 `json:"total"`
Available uint64 `json:"available"`
@ -22,3 +26,13 @@ type SwapMemoryStat struct {
Sin uint64 `json:"sin"`
Sout uint64 `json:"sout"`
}
func (m VirtualMemoryStat) String() string {
s, _ := json.Marshal(m)
return string(s)
}
func (m SwapMemoryStat) String() string {
s, _ := json.Marshal(m)
return string(s)
}

@ -1,7 +1,6 @@
package gopsutil
import (
"encoding/json"
"fmt"
"testing"
)
@ -11,8 +10,7 @@ func TestVirtual_memory(t *testing.T) {
if err != nil {
t.Errorf("error %v", err)
}
d, _ := json.Marshal(v)
fmt.Printf("%s\n", d)
fmt.Println(v)
}
func TestSwap_memory(t *testing.T) {
@ -20,6 +18,5 @@ func TestSwap_memory(t *testing.T) {
if err != nil {
t.Errorf("error %v", err)
}
d, _ := json.Marshal(v)
fmt.Printf("%s\n", d)
fmt.Println(v)
}

@ -1,5 +1,9 @@
package gopsutil
import (
"encoding/json"
)
type NetIOCountersStat struct {
Name string `json:"name"` // interface name
BytesSent uint64 `json:"bytes_sent"` // number of bytes sent
@ -26,3 +30,18 @@ type NetConnectionStat struct {
Status string `json:"status"`
Pid int32 `json:"pid"`
}
func (n NetConnectionStat) String() string {
s, _ := json.Marshal(n)
return string(s)
}
func (n NetIOCountersStat) String() string {
s, _ := json.Marshal(n)
return string(s)
}
func (a Addr) String() string {
s, _ := json.Marshal(a)
return string(s)
}

@ -1,5 +1,9 @@
package gopsutil
import (
"encoding/json"
)
type Process struct {
Pid int32 `json:"pid"`
}
@ -27,6 +31,31 @@ type IOCountersStat struct {
WriteBytes int32 `json:"write_bytes"`
}
func (p Process) String() string {
s, _ := json.Marshal(p)
return string(s)
}
func (o OpenFilesStat) String() string {
s, _ := json.Marshal(o)
return string(s)
}
func (m MemoryInfoStat) String() string {
s, _ := json.Marshal(m)
return string(s)
}
func (r RlimitStat) String() string {
s, _ := json.Marshal(r)
return string(s)
}
func (i IOCountersStat) String() string {
s, _ := json.Marshal(i)
return string(s)
}
func PidExists(pid int32) (bool, error) {
pids, err := Pids()
if err != nil {

@ -1,7 +1,6 @@
package gopsutil
import (
"encoding/json"
"fmt"
"os"
"runtime"
@ -54,8 +53,7 @@ func Test_NewProcess(t *testing.T) {
t.Errorf("error %v", err)
}
d, _ := json.Marshal(ret)
fmt.Println(string(d))
fmt.Println(ret)
}
func Test_Process_memory_maps(t *testing.T) {

Loading…
Cancel
Save