Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/zhuliquan/go_tools
some go tools for my develop
https://github.com/zhuliquan/go_tools
Last synced: about 2 months ago
JSON representation
some go tools for my develop
- Host: GitHub
- URL: https://github.com/zhuliquan/go_tools
- Owner: zhuliquan
- License: mit
- Created: 2021-03-21T04:14:14.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2021-03-21T04:15:09.000Z (almost 4 years ago)
- Last Synced: 2023-07-27T22:49:28.122Z (over 1 year ago)
- Language: Go
- Size: 5.86 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# go_tools
---
go_tools is go package for go develop, include ip/ip cidr...
## Features
---
### 1. ip_tools
- go_tools/ip_tools can convert string ip to []byte.
- go_tools/ip_tools can generate random ip / ip cidr.
- go_tools/ip_tools can generate random ip between two ips.
- go_tools/ip_tools can calculate first/last valid ip, boardcase address, network address according to ip cidr.
## Install
---
```go get -v github.com/zhuliquan/go_tools```## Example
---
1. useage of ip_tools
```golang
package mainimport (
"fmt"
"github.com/zhuliquan/go_tools/ip_tools"
)func main() {
if err := ip_tools.CheckValidIp("123.0.0.1"); err == nil {
fmt.Println("this is valid ip")
}
if err := ip_tools.CheckValidIp("123.0.1"); err != nil {
fmt.Println("this is not valid ip")
}
if err := ip_tools.CheckValidIpCidr("126.0.0.8/4"); err != nil {
fmt.Println("this is valid ip cidr")
}
if err := ip_tools.CheckValidIpCidr("126.0.0.9/800"); err != nil {
fmt.Println("this is not valid ip cidr")
}
ip := ip_tools.GetRandomIpV4()
fmt.Println("get a random ip v4: ", ip)
ip = ip_tools.GetRandomIpV6()
fmt.Println("get a random ip v6: ", ip)
ipCidr := ip_tools.GetRandomIpCidrV4()
fmt.Printf("Get a random ip cidr v4: %s \n", ipCidr)
fmt.Printf("accroding to ip cidr %s, network address: %+v\n", ipCidr, ipCidr.GetNetworkAddress())
fmt.Printf("accroding to ip cidr %s, broadcast address: %+v\n", ipCidr, ipCidr.GetBroadCastAddress())
fmt.Printf("accroding to ip cidr %s, first valid address: %+v\n", ipCidr, ipCidr.GetFirstValidIp())
fmt.Printf("accroding to ip cidr %s, last valid address: %+v\n", ipCidr, ipCidr.GetLastValidIp())
fmt.Printf("accroding to ip cidr %s, we can get random contained ip: %+v\n", ipCidr, ipCidr.GetRandomValidIp())
ipCidr = ip_tools.GetRandomIpCidrV6()
fmt.Println("Get a random ip cidr v6: ", ipCidr)
fmt.Printf("accroding to ip cidr %s, network address: %+v\n", ipCidr, ipCidr.GetNetworkAddress())
fmt.Printf("accroding to ip cidr %s, broadcast address: %+v\n", ipCidr, ipCidr.GetBroadCastAddress())
fmt.Printf("accroding to ip cidr %s, first valid address: %+v\n", ipCidr, ipCidr.GetFirstValidIp())
fmt.Printf("accroding to ip cidr %s, last valid address: %+v\n", ipCidr, ipCidr.GetLastValidIp())
fmt.Printf("accroding to ip cidr %s, we can get random contained ip: %+v\n", ipCidr, ipCidr.GetRandomValidIp())
}```