From f73c7e7e868f03ce21918945667271c0b2e6ae9a Mon Sep 17 00:00:00 2001 From: dudaodong Date: Wed, 19 Apr 2023 11:48:28 +0800 Subject: [PATCH] doc: add document and example for IsPingConnected and IsTelnetConnected --- docs/netutil.md | 84 ++++++++++++++++++++++++++++++++++--- docs/netutil_zh-CN.md | 74 ++++++++++++++++++++++++++++++++ netutil/net_example_test.go | 24 +++++++++++ 3 files changed, 177 insertions(+), 5 deletions(-) diff --git a/docs/netutil.md b/docs/netutil.md index eb82cc9..90c3578 100644 --- a/docs/netutil.md +++ b/docs/netutil.md @@ -44,6 +44,8 @@ import ( - [HttpPutDeprecated](#HttpPut) - [HttpPatchDeprecated](#HttpPatch) - [ParseHttpResponse](#ParseHttpResponse) +- [IsPingConnected](#IsPingConnected) +- [IsTelnetConnected](#IsTelnetConnected)
@@ -110,8 +112,8 @@ func main() { fmt.Println(err) } - fmt.Println(encodedUrl) - + fmt.Println(encodedUrl) + // Output: // http://www.lancet.com?a=1&b=%5B2%5D } @@ -142,8 +144,8 @@ func main() { internalIp := netutil.GetInternalIp() ip := net.ParseIP(internalIp) - fmt.Println(ip) - + fmt.Println(ip) + // Output: // 192.168.1.9 } @@ -172,7 +174,7 @@ import ( func main() { ips := netutil.GetIps() - fmt.Println(ips) + fmt.Println(ips) // Output: // [192.168.1.9] @@ -896,3 +898,75 @@ func main() { fmt.Println(toDoResp) } ``` + +### IsPingConnected + +

checks if can ping the specified host or not.

+ +Signature: + +```go +func IsPingConnected(host string) bool +``` + +Example: + +```go +package main + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "log" + "github.com/duke-git/lancet/v2/netutil" +) + +func main() { + result1 := netutil.IsPingConnected("www.baidu.com") + result2 := netutil.IsPingConnected("www.!@#&&&.com") + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // true + // false +} +``` + +### IsTelnetConnected + +

Checks if can telnet the specified host or not.

+ +Signature: + +```go +func IsTelnetConnected(host string, port string) bool +``` + +Example: + +```go +package main + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "log" + "github.com/duke-git/lancet/v2/netutil" +) + +func main() { + result1 := netutil.IsTelnetConnected("www.baidu.com", "80") + result2 := netutil.IsTelnetConnected("www.baidu.com", "123") + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // true + // false +} +``` diff --git a/docs/netutil_zh-CN.md b/docs/netutil_zh-CN.md index c9a007f..62e8c23 100644 --- a/docs/netutil_zh-CN.md +++ b/docs/netutil_zh-CN.md @@ -44,6 +44,8 @@ import ( - [HttpPutDeprecated](#HttpPut) - [HttpPatchDeprecated](#HttpPatch) - [ParseHttpResponse](#ParseHttpResponse) +- [IsPingConnected](#IsPingConnected) +- [IsTelnetConnected](#IsTelnetConnected)
@@ -898,3 +900,75 @@ func main() { fmt.Println(toDoResp) } ``` + +### IsPingConnected + +

检查能否ping通主机。

+ +函数签名: + +```go +func IsPingConnected(host string) bool +``` + +示例: + +```go +package main + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "log" + "github.com/duke-git/lancet/v2/netutil" +) + +func main() { + result1 := netutil.IsPingConnected("www.baidu.com") + result2 := netutil.IsPingConnected("www.!@#&&&.com") + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // true + // false +} +``` + +### IsTelnetConnected + +

检查能否telnet到主机。

+ +函数签名: + +```go +func IsTelnetConnected(host string, port string) bool +``` + +示例: + +```go +package main + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "log" + "github.com/duke-git/lancet/v2/netutil" +) + +func main() { + result1 := netutil.IsTelnetConnected("www.baidu.com", "80") + result2 := netutil.IsTelnetConnected("www.baidu.com", "123") + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // true + // false +} +``` diff --git a/netutil/net_example_test.go b/netutil/net_example_test.go index 04721d3..e9454ab 100644 --- a/netutil/net_example_test.go +++ b/netutil/net_example_test.go @@ -178,3 +178,27 @@ func ExampleConvertMapToQueryString() { // Output: // a=1&b=2&c=3 } + +func ExampleIsPingConnected() { + result1 := IsPingConnected("www.baidu.com") + result2 := IsPingConnected("www.!@#&&&.com") + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // true + // false +} + +func ExampleIsTelnetConnected() { + result1 := IsTelnetConnected("www.baidu.com", "80") + result2 := IsTelnetConnected("www.baidu.com", "123") + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // true + // false +}