-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathinterface_list.go
60 lines (44 loc) · 1.04 KB
/
interface_list.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package main
import (
"net"
"sync"
)
type InterfaceList struct {
mu sync.RWMutex
interfaces []net.Interface
}
func NewInterfaceList() *InterfaceList {
return &InterfaceList{interfaces: make([]net.Interface, 0)}
}
func (il *InterfaceList) ClearAndAppend(interfaces []net.Interface) {
il.mu.Lock()
defer il.mu.Unlock()
il.interfaces = make([]net.Interface, 0)
for _, iface := range interfaces {
il.interfaces = append(il.interfaces, iface)
}
}
func (il *InterfaceList) Append(iface net.Interface) {
il.mu.Lock()
defer il.mu.Unlock()
il.interfaces = append(il.interfaces, iface)
}
func (il *InterfaceList) Get(i int) net.Interface {
il.mu.RLock()
defer il.mu.RUnlock()
return il.interfaces[i]
}
func (il *InterfaceList) All() []net.Interface {
il.mu.RLock()
defer il.mu.RUnlock()
interfaces := make([]net.Interface, len(il.interfaces))
for i, iface := range il.interfaces {
interfaces[i] = iface
}
return interfaces
}
func (il *InterfaceList) Len() int {
il.mu.RLock()
defer il.mu.RUnlock()
return len(il.interfaces)
}