Skip to content

Commit cf17adf

Browse files
committed
feat: netutil - add new func: IncrIP
1 parent dfc6cb6 commit cf17adf

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

netutil/iputil.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package netutil
2+
3+
import "net"
4+
5+
// IncrIP 将IP地址递增1 eg: 192.168.1.1 -> 192.168.1.2
6+
func IncrIP(ip net.IP) {
7+
for j := len(ip) - 1; j >= 0; j-- {
8+
ip[j]++
9+
if ip[j] > 0 {
10+
break
11+
}
12+
}
13+
}
14+

netutil/iputil_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package netutil_test
2+
3+
import (
4+
"net"
5+
"testing"
6+
7+
"github.com/gookit/goutil/netutil"
8+
"github.com/gookit/goutil/testutil/assert"
9+
)
10+
11+
func TestIncrIP(t *testing.T) {
12+
ip := net.IP{192, 168, 1, 1}
13+
netutil.IncrIP(ip)
14+
assert.Eq(t, "192.168.1.2", ip.String())
15+
}

0 commit comments

Comments
 (0)