Multiple ways to get Hostname and IP address in golang programs code

Golang net package

The hostname is a label assigned to a machine that is connected to a network of machines. Golang standard package net provides functions related to TCP/IP, UDP, and DNS related networking related functionalities

The following are various examples related to the hostname and Ip addresses in Go Language.

How to Get the Hostname of the System environment in Golang?

It is always good to know the hostname of the environment where they go program is running. standard package inbuilt os provides a hostname function which gives the hostname of the environment. You can check here🔗 for more information
Here is the syntax of the hostname function

func Hostname() (name string, error error)

It returns

  • name - hostname and error return nil if no error exists For example, if you are running on a System named ‘system1’, os.Hostname() function returns ‘system1’ as an output.

Following is an example of finding the hostname of the System

package main

import (
    "fmt"
    "os"
)

func main() {
    hostname, error: = os.Hostname()
    if error != nil {
        panic(error)
    }
    fmt.Println("hostname returned from Environment : ", hostname)
    fmt.Println("error : ", error)

}

Output:

hostname returned from Environment :  DESKTOP-47884kl
error :  <nil>

How to Check a string containing an IP address Hostname or domain name Golang?

This program is to check whether a given string is an IP address or hostname.

net package provides ParseIP() function for checking IP address with the format of IPV4 (192.134.1.12) and IPV6 (1111:ca7::57).

Here is a syntax of the ParseIP() function

func ParseIP(s string) IP

if the given string is not a valid IP address representation, It returns nil.

Here is a program for checking the hostname or IP address of a given string

package main

import (
    "fmt"
    "net"
)

func checkStringForIpOrHostname(host string) {
    addr: = net.ParseIP(host)
    if addr == nil {
        fmt.Println("Given String is a Domain Name")

    } else {
        fmt.Println("Given String is a Ip Address")
    }
}
func main() {
    checkStringForIpOrHostname("google.com")
    checkStringForIpOrHostname("192.168.1.0")

}

Output:

Given String is a Domain Name
Given String is an Ip Address

How to get a Domain name from a given URL Golang?

Often, we need to check the Domain name from the given URL.

For example, if Url is like mydomain.com/category/mypage.html, the Domain name should return mydomain.com

Package URL provides a function parse() that checks the URL and returns the URL instance. Hostname with URL instance return hostname.

Here is parse function syntax

func Parse(rawurl string) (*URL, error)

It takes rawurl as input string, returns URL instance an error object

package main

import (
    "fmt"
    "net/url"
)

func main() {
    urlInstance, error: = url.Parse("https://mydomain.com/category/mypage.html")
    if error != nil {
        fmt.Println(error)
    }
    fmt.Println(urlInstance.Hostname())
}

Output:

mydomain.com

How to get a Fully Qualified Domain Name - FQDN of a local machine golang?

There is no direct library to get the FQDN of a machine. There is a custom wrapper library https://github.com/Showmax/go-fqdn🔗 based on OS and net package. First, install this library using the below command

 go get github.com/Showmax/go-fqdn

The following program returns a fully qualified domain name if fqdn exists, else returns an unknown.

package main

import (
    "fmt"

    fqdn "github.com/Showmax/go-fqdn"
)

func main() {
    fmt.Println(fqdn.Get())
}

Output is

DESKTOP-968A6TP

How to get the local IP address of a system Golang?

the net package provides the Dial function which connects to the target address of the named network. UDP is a protocol that doesn’t require a physical connection like TCP and the target will not connect to it

package main

import (
    "fmt"
    "net"
)

func main() {
    conn, error: = net.Dial("udp", "8.8.8.8:80")
    if error != nil {
        fmt.Println(error)

    }

    defer conn.Close()
    ipAddress: = conn.LocalAddr().( * net.UDPAddr)
    fmt.Println(ipAddress)
}

Output:

192.168.1.102:49703

Conclusion

In this tutorial, You learned how to get the domain name, Qualified Domain Name, and IP address of a system in a Go Language.