Skip to content

Commit 3ff690d

Browse files
committed
make single network interface about NetIOCounters on darwin and freebsd
1 parent 406cbe9 commit 3ff690d

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

net/net_darwin.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"os/exec"
77
"strconv"
88
"strings"
9+
10+
"github.com/shirou/gopsutil/common"
911
)
1012

1113
func NetIOCounters(pernic bool) ([]NetIOCountersStat, error) {
@@ -16,13 +18,20 @@ func NetIOCounters(pernic bool) ([]NetIOCountersStat, error) {
1618

1719
lines := strings.Split(string(out), "\n")
1820
ret := make([]NetIOCountersStat, 0, len(lines)-1)
21+
exists := make([]string, 0, len(ret))
1922

2023
for _, line := range lines {
2124
values := strings.Fields(line)
2225
if len(values) < 1 || values[0] == "Name" {
2326
// skip first line
2427
continue
2528
}
29+
if common.StringContains(exists, values[0]) {
30+
// skip if already get
31+
continue
32+
}
33+
exists = append(exists, values[0])
34+
2635
base := 1
2736
// sometimes Address is ommitted
2837
if len(values) < 11 {

net/net_freebsd.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"os/exec"
77
"strconv"
88
"strings"
9+
10+
"github.com/shirou/gopsutil/common"
911
)
1012

1113
func NetIOCounters(pernic bool) ([]NetIOCountersStat, error) {
@@ -16,12 +18,19 @@ func NetIOCounters(pernic bool) ([]NetIOCountersStat, error) {
1618

1719
lines := strings.Split(string(out), "\n")
1820
ret := make([]NetIOCountersStat, 0, len(lines)-1)
21+
exists := make([]string, 0, len(ret))
1922

2023
for _, line := range lines {
2124
values := strings.Fields(line)
2225
if len(values) < 1 || values[0] == "Name" {
2326
continue
2427
}
28+
if common.StringContains(exists, values[0]) {
29+
// skip if already get
30+
continue
31+
}
32+
exists = append(exists, values[0])
33+
2534
base := 1
2635
// sometimes Address is ommitted
2736
if len(values) < 13 {

0 commit comments

Comments
 (0)