How to Validate an IP Address in Golang

The challenge

Write an algorithm that will identify valid IPv4 addresses in dot-decimal format. IPs should be considered valid if they consist of four octets, with values between 0 and 255, inclusive.

Valid inputs examples:

1.2.3.4 123.45.67.89
Code language: CSS (css)

Invalid input examples:

1.2.3 1.2.3.4.5 123.456.78.90 123.045.067.089
Code language: CSS (css)

Notes:

  • Leading zeros (e.g. 01.02.03.04) are considered invalid
  • Inputs are guaranteed to be a single string

The solution in Golang

Option 1:

package solution import "net" func Is_valid_ip(ip string) bool { if r := net.ParseIP(ip); r == nil { return false } return true }
Code language: Go (go)

Option 2:

package solution import "net" func Is_valid_ip(ip string) bool { return net.ParseIP(ip) != nil }
Code language: Go (go)

Option 3:

package solution 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 }
Code language: Go (go)

Test cases to validate our solution

package solution_test import ( . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" ) var _ = Describe("Test Example", func() { It("should test that 12.255.56.1 is correct", func() { Expect(Is_valid_ip("12.255.56.1")).To(Equal(true)) }) }) var _ = Describe("Test Example", func() { It("should test that '' is uncorrect", func() { Expect(Is_valid_ip("")).To(Equal(false)) }) }) var _ = Describe("Test Example", func() { It("should test that abc.def.ghi.jkl is uncorrect", func() { Expect(Is_valid_ip("abc.def.ghi.jkl")).To(Equal(false)) }) }) var _ = Describe("Test Example", func() { It("should test that 123.456.789.0 is uncorrect", func() { Expect(Is_valid_ip("123.456.789.0")).To(Equal(false)) }) }) var _ = Describe("Test Example", func() { It("should test that 12.34.56 is uncorrect", func() { Expect(Is_valid_ip("12.34.56")).To(Equal(false)) }) }) var _ = Describe("Test Example", func() { It("should test that 12.34.56 .1 is uncorrect", func() { Expect(Is_valid_ip("12.34.56 .1")).To(Equal(false)) }) }) var _ = Describe("Test Example", func() { It("should test that 12.34.56.-1 is uncorrect", func() { Expect(Is_valid_ip("12.34.56.-1")).To(Equal(false)) }) }) var _ = Describe("Test Example", func() { It("should test that 123.045.067.089 is uncorrect", func() { Expect(Is_valid_ip("123.045.067.089")).To(Equal(false)) }) }) var _ = Describe("Test Example", func() { It("should test that 127.1.1.0 is correct", func() { Expect(Is_valid_ip("127.1.1.0")).To(Equal(true)) }) }) var _ = Describe("Test Example", func() { It("should test that 0.0.0.0 is correct", func() { Expect(Is_valid_ip("0.0.0.0")).To(Equal(true)) }) }) var _ = Describe("Test Example", func() { It("should test that 0.34.82.53 is correct", func() { Expect(Is_valid_ip("0.34.82.53")).To(Equal(true)) }) }) var _ = Describe("Test Example", func() { It("should test that 192.168.1.300 is uncorrect", func() { Expect(Is_valid_ip("192.168.1.300")).To(Equal(false)) }) })
Code language: Go (go)
Tags:
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments