How to resolve the algorithm MAC vendor lookup step by step in the Go programming language

Published on 12 May 2024 09:40 PM
#Go

How to resolve the algorithm MAC vendor lookup step by step in the Go programming language

Table of Contents

Problem Statement

Every connected device around the world comes with a unique Media Access Control address, or a   MAC address. A common task a network administrator may come across is being able to identify a network device's manufacturer when given only a MAC address.

Interface with one (or numerous) APIs that exist on the internet and retrieve the device manufacturer based on a supplied MAC address. A MAC address that does not return a valid result should return the String "N/A".   An error related to the network connectivity or the API should return a null result. Many implementations on this page use http://api.macvendors.com/ which, as of 19th September 2021, is throttling requests. After only 2 calls, the following response is returned for all subsequent requests. If you are planning to use the same provider or going to run the examples on this page, consider building in a delay between two calls. {"errors":{"detail":"Too Many Requests","message":"Please slow down your requests or upgrade your plan at https://macvendors.com"}}

Let's start with the solution:

Step by Step solution about How to resolve the algorithm MAC vendor lookup step by step in the Go programming language

This Go program performs MAC address lookups using an external API and prints the results. Here's a detailed explanation of the code:

  1. Package Declaration: The program starts by declaring the main package, which is the entry point for Go programs.

  2. Import Statements: The program imports the following standard library packages:

    • net/http: For making HTTP requests to the MAC address lookup API.
    • fmt: For printing the results.
    • io/ioutil: For reading the response body from the API.
  3. macLookUp Function: This function takes a MAC address as a string parameter and returns the corresponding vendor name as a string.

    • It sends an HTTP GET request to the "http://api.macvendors.com/" URL, appending the provided MAC address to the URL path.
    • It reads the response body using ioutil.ReadAll and stores it in the body variable.
    • Finally, it converts the response body into a string and returns it.
  4. main Function:

    • This function is the starting point of the program.
    • It calls the macLookUp function three times, each with a different MAC address.
    • The results are printed to the console using fmt.Println.

In summary, this program uses the macvendors.com API to look up vendor names based on MAC addresses and prints the results.

Source code in the go programming language

package main

import (
	"net/http"
	"fmt"
	"io/ioutil"
)

func macLookUp(mac string) (res string){
	resp, _ := http.Get("http://api.macvendors.com/" + mac)
	body, _ := ioutil.ReadAll(resp.Body)
	res = string(body)
	return
}

func main()  {
	fmt.Println(macLookUp("FC-A1-3E"))
	fmt.Println(macLookUp("FC:FB:FB:01:FA:21"))
	fmt.Println(macLookUp("BC:5F:F4"))
}


  

You may also check:How to resolve the algorithm Compound data type step by step in the Logo programming language
You may also check:How to resolve the algorithm Random number generator (device) step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Narcissistic decimal number step by step in the Ruby programming language
You may also check:How to resolve the algorithm Narcissistic decimal number step by step in the Clojure programming language
You may also check:How to resolve the algorithm Four bit adder step by step in the Ring programming language