Wednesday, December 6, 2023
HomeSoftware EngineeringValidate an IP Handle in Golang

# Validate an IP Handle in Golang

## The problem#

Write an algorithm that can determine legitimate IPv4 addresses in dot-decimal format. IPs must be thought of legitimate in the event that they consist of 4 octets, with values between “ and 255, inclusive.

Legitimate inputs examples:

1.2.3.4
123.45.67.89

Invalid enter examples:

1.2.3
1.2.3.4.5
123.456.78.90
123.045.067.089

Notes:

• Main zeros (e.g. 01.02.03.04) are thought of invalid
• Inputs are assured to be a single string

## The answer in Golang#

Possibility 1:

bundle answer
import "web"
func Is_valid_ip(ip string) bool {
if r := web.ParseIP(ip); r == nil {
return false
}
return true
}

Possibility 2:

bundle answer
import "web"
func Is_valid_ip(ip string) bool {
return web.ParseIP(ip) != nil
}

Possibility 3:

bundle answer
import "regexp"
func Is_valid_ip(ip string) bool {
re, _ := regexp.Compile(`^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\$`)
if re.MatchString(ip) {
return true
}
return false
}

## Check circumstances to validate our answer#

bundle solution_test

import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Check Instance", func() {
It("ought to check that 12.255.56.1 is right", func() {
Anticipate(Is_valid_ip("12.255.56.1")).To(Equal(true))
})
})

var _ = Describe("Check Instance", func() {
It("ought to check that '' is uncorrect", func() {
Anticipate(Is_valid_ip("")).To(Equal(false))
})
})

var _ = Describe("Check Instance", func() {
It("ought to check that abc.def.ghi.jkl is uncorrect", func() {
Anticipate(Is_valid_ip("abc.def.ghi.jkl")).To(Equal(false))
})
})

var _ = Describe("Check Instance", func() {
It("ought to check that 123.456.789.0 is uncorrect", func() {
Anticipate(Is_valid_ip("123.456.789.0")).To(Equal(false))
})
})

var _ = Describe("Check Instance", func() {
It("ought to check that 12.34.56 is uncorrect", func() {
Anticipate(Is_valid_ip("12.34.56")).To(Equal(false))
})
})

var _ = Describe("Check Instance", func() {
It("ought to check that 12.34.56 .1 is uncorrect", func() {
Anticipate(Is_valid_ip("12.34.56 .1")).To(Equal(false))
})
})

var _ = Describe("Check Instance", func() {
It("ought to check that 12.34.56.-1 is uncorrect", func() {
Anticipate(Is_valid_ip("12.34.56.-1")).To(Equal(false))
})
})

var _ = Describe("Check Instance", func() {
It("ought to check that 123.045.067.089 is uncorrect", func() {
Anticipate(Is_valid_ip("123.045.067.089")).To(Equal(false))
})
})

var _ = Describe("Check Instance", func() {
It("ought to check that 127.1.1.0 is right", func() {
Anticipate(Is_valid_ip("127.1.1.0")).To(Equal(true))
})
})

var _ = Describe("Check Instance", func() {
It("ought to check that 0.0.0.0 is right", func() {
Anticipate(Is_valid_ip("0.0.0.0")).To(Equal(true))
})
})

var _ = Describe("Check Instance", func() {
It("ought to check that 0.34.82.53 is right", func() {
Anticipate(Is_valid_ip("0.34.82.53")).To(Equal(true))
})
})

var _ = Describe("Check Instance", func() {
It("ought to check that 192.168.1.300 is uncorrect", func() {
Anticipate(Is_valid_ip("192.168.1.300")).To(Equal(false))
})
})
RELATED ARTICLES