Wednesday, February 21, 2024
HomeSoftware EngineeringMethods to Validate an IP Tackle in Golang

# Methods to Validate an IP Tackle in Golang

## The problem#

Write an algorithm that may determine legitimate IPv4 addresses in dot-decimal format. IPs needs to 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

Possibility 1:

``````package deal answer
import "internet"
func Is_valid_ip(ip string) bool {
if r := internet.ParseIP(ip); r == nil {
return false
}
return true
}
``````

Possibility 2:

``````package deal answer
import "internet"
func Is_valid_ip(ip string) bool {
return internet.ParseIP(ip) != nil
}
``````

Possibility 3:

``````package deal 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
}
``````

## Take a look at circumstances to validate our answer#

``````package deal solution_test

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

var _ = Describe("Take a look at Instance", func() {
It("ought to take a look at that '' is uncorrect", func() {
Anticipate(Is_valid_ip("")).To(Equal(false))
})
})

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

var _ = Describe("Take a look at Instance", func() {
It("ought to take a look at that 123.456.789.0 is uncorrect", func() {
Anticipate(Is_valid_ip("123.456.789.0")).To(Equal(false))
})
})

var _ = Describe("Take a look at Instance", func() {
It("ought to take a look at that 12.34.56 is uncorrect", func() {
Anticipate(Is_valid_ip("12.34.56")).To(Equal(false))
})
})

var _ = Describe("Take a look at Instance", func() {
It("ought to take a look at that 12.34.56 .1 is uncorrect", func() {
Anticipate(Is_valid_ip("12.34.56 .1")).To(Equal(false))
})
})

var _ = Describe("Take a look at Instance", func() {
It("ought to take a look at that 12.34.56.-1 is uncorrect", func() {
Anticipate(Is_valid_ip("12.34.56.-1")).To(Equal(false))
})
})

var _ = Describe("Take a look at Instance", func() {
It("ought to take a look at that 123.045.067.089 is uncorrect", func() {
Anticipate(Is_valid_ip("123.045.067.089")).To(Equal(false))
})
})

var _ = Describe("Take a look at Instance", func() {
It("ought to take a look at that 127.1.1.0 is right", func() {
Anticipate(Is_valid_ip("127.1.1.0")).To(Equal(true))
})
})

var _ = Describe("Take a look at Instance", func() {
It("ought to take a look at that 0.0.0.0 is right", func() {
Anticipate(Is_valid_ip("0.0.0.0")).To(Equal(true))
})
})

var _ = Describe("Take a look at Instance", func() {
It("ought to take a look at that 0.34.82.53 is right", func() {
Anticipate(Is_valid_ip("0.34.82.53")).To(Equal(true))
})
})

var _ = Describe("Take a look at Instance", func() {
It("ought to take a look at that 192.168.1.300 is uncorrect", func() {
Anticipate(Is_valid_ip("192.168.1.300")).To(Equal(false))
})
})
``````
RELATED ARTICLES