Add u.Hostname() (Go 1.8+) to url.Parse example

This commit is contained in:
Willem de Groot 2022-06-09 15:20:41 +02:00 committed by GitHub
parent c662818d23
commit 28a7c62c00
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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)