-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pull request: netutil: add more utilites
Merge in DNS/golibs from imp-netutil to master Squashed commit of the following: commit 748618c Author: Ainar Garipov <[email protected]> Date: Wed Aug 4 18:10:10 2021 +0300 netutil: imp tests, docs commit 9aec541 Author: Ainar Garipov <[email protected]> Date: Wed Aug 4 17:38:08 2021 +0300 netutil: add more utilites
- Loading branch information
Showing
4 changed files
with
537 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package netutil_test | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/AdguardTeam/golibs/netutil" | ||
) | ||
|
||
func ExampleJoinHostPort() { | ||
fmt.Println(netutil.JoinHostPort("example.com", 12345)) | ||
|
||
// Output: | ||
// | ||
// example.com:12345 | ||
} | ||
|
||
func ExampleParseIP() { | ||
ip, err := netutil.ParseIP("1.2.3.4") | ||
fmt.Println(ip, err) | ||
|
||
ip, err = netutil.ParseIP("1234::cdef") | ||
fmt.Println(ip, err) | ||
|
||
ip, err = netutil.ParseIP("!!!") | ||
fmt.Println(ip, err) | ||
|
||
// Output: | ||
// | ||
// 1.2.3.4 <nil> | ||
// 1234::cdef <nil> | ||
// <nil> bad ip address "!!!" | ||
} | ||
|
||
func ExampleParseIPv4() { | ||
ip, err := netutil.ParseIPv4("1.2.3.4") | ||
fmt.Println(ip, err) | ||
|
||
ip, err = netutil.ParseIPv4("1234::cdef") | ||
fmt.Println(ip, err) | ||
|
||
ip, err = netutil.ParseIPv4("!!!") | ||
fmt.Println(ip, err) | ||
|
||
// Output: | ||
// | ||
// 1.2.3.4 <nil> | ||
// <nil> bad ipv4 address "1234::cdef" | ||
// <nil> bad ip address "!!!" | ||
} | ||
|
||
func ExampleSplitHostPort() { | ||
host, port, err := netutil.SplitHostPort("example.com:12345") | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
fmt.Printf("%T(%[1]v)\n", host) | ||
fmt.Printf("%T(%[1]v)\n", port) | ||
|
||
// Output: | ||
// | ||
// string(example.com) | ||
// int(12345) | ||
} | ||
|
||
func ExampleSplitHost() { | ||
host, err := netutil.SplitHost("example.com:12345") | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
fmt.Println(host) | ||
|
||
host, err = netutil.SplitHost("example.org") | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
fmt.Println(host) | ||
|
||
_, err = netutil.SplitHost("[BAD:!") | ||
fmt.Println(err) | ||
|
||
// Output: | ||
// | ||
// example.com | ||
// example.org | ||
// address [BAD:!: missing ']' in address | ||
} |
Oops, something went wrong.