From 28a7c62c003b987f8af43b96c86d724023ffe83b Mon Sep 17 00:00:00 2001 From: Willem de Groot Date: Thu, 9 Jun 2022 15:20:41 +0200 Subject: [PATCH] Add u.Hostname() (Go 1.8+) to url.Parse example --- examples/url-parsing/url-parsing.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/examples/url-parsing/url-parsing.go b/examples/url-parsing/url-parsing.go index f7590f1..592a7e5 100644 --- a/examples/url-parsing/url-parsing.go +++ b/examples/url-parsing/url-parsing.go @@ -34,9 +34,12 @@ func main() { fmt.Println(p) // The `Host` contains both the hostname and the port, - // if present. Use `SplitHostPort` to extract them. + // if present. Use `Hostname()` to get just the hostname. + // Use `SplitHostPort` to extract host and port (fails + // if port is missing). fmt.Println(u.Host) - host, port, _ := net.SplitHostPort(u.Host) + fmt.Println(u.Hostname() + host, port, err := net.SplitHostPort(u.Host) fmt.Println(host) fmt.Println(port)