{

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 Hostname of the System environment?

It is always good to know the hostname of the environment where they go program is running. standard package inbuilt os provides hostname function which gives hostname of the environment. You can check here for more information
Here is the syntax of 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 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 is

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

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

This program is to check a given string is 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 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?

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() checks URL and returns 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 is

  
mydomain.com  

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

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 

Following program returns fully qualified domain name if fqdn exists, else return 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?

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 target will not connect 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 is

192.168.1.102:49703  

Conclusion

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

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.