Menu Close

Go – SplitHostPort() function in net package in go

Here, we will learn about how to split network address into host and port in go by using SplitHostPort() function in net package in go.

SplitHostPort() function in net package splits a network address of the form “host:port“, “host%zone:port“, “[host]:port” or “[host%zone]:port” into host or host%zone and port.

If you want to split IPV6 address by using SplitHostPort() function then IPv6 address in hostport must be enclosed in square brackets, as in "[::1]:80", "[::1%lo0]:80".

How SplitHostPort works?

The SplitHostPort function takes a string containing a network address as its input, and returns two strings: the host and the port. The function uses the colon (:) character as the separator between the host and port components of the address. If the input string does not contain a colon, the host component is set to the input string and the port component is set to an empty string.

Function prototype:

func SplitHostPort(hostport string) (host, port string, err error)

Return:

host : Host address / name
port : Port number
err : Error message

Example 1:

package main

import (
    "fmt"
    "net"
)

func main() {
    host_port := "techieindoor.com:80"
    
    host, port, err := net.SplitHostPort(host_port)
    
    if err != nil {
        fmt.Println(err)
    }
    
    fmt.Println("Host: ", host, "\n", "Port: ", port)
}

Output:

Host: techieindoor.com
Port: 80

Example 2:

package main

import (
    "fmt"
    "net"
    "net/url"
    "log"
)

func main() {
    url_str := "https://www.techieindoor.com:8000/go-net-package-in-go-golang/"
    
    url, err := url.Parse(url_str)
    
    if err != nil {
        log.Fatal(err)
    }
 
    host, port, err := net.SplitHostPort(url.Host)
    
    if err != nil {
        log.Fatal(err)
    }
    
    fmt.Println("Host: ", host, "\n", "Port: ", port)
}

Output:

Host:  www.techieindoor.com 
Port:  8000

Example 3:

package main

import (
    "fmt"
    "net"
)

func main() {
    address := "techieindoor.com"
    
    host, port, err := net.SplitHostPort(address)
    
    if err != nil {
        fmt.Println("Error:", err)
    } else {
        fmt.Println("Host:", host)
        fmt.Println("Port:", port)
    }
}

Output:

Host: example.com
Port:

In this example, we pass a string containing a network address to SplitHostPort, but this address does not contain a port component. In this case, the SplitHostPort function sets the host component to the input string and the port component to an empty string.

Parse IPv6 address with port number.

package main

import (
    "fmt"
    "net"
    "net/url"
    "log"
)

func main() {
    url_str := "http://[2001:db8::1]:8080/"
    
    url, err := url.Parse(url_str)
    
    if err != nil {
        log.Fatal(err)
    }
 
    host, port, err := net.SplitHostPort(url.Host)
    
    if err != nil {
        log.Fatal(err)
    }
    
    fmt.Println("Host: ", host, "\n", "Port: ", port)
}

Output:

Host:  2001:db8::1 
Port:  8080

To learn more about golang, Please refer given below link:

https://www.techieindoor.com/category/golang/packages/net/

References:

https://golang.org/

Posted in Easy, golang, Level, net

Leave a Reply

Your email address will not be published. Required fields are marked *