Software Engineering

Easy methods to create a Coordinates Validator in Golang

Easy methods to create a Coordinates Validator in Golang
Written by admin


The problem

You’ll want to create a operate that may validate if given parameters are legitimate geographical coordinates.

Legitimate coordinates appear to be the next: “23.32353342, -32.543534534”. The return worth must be both true or false.

Latitude (which is the primary float) might be between 0 and 90, optimistic or unfavourable. Longitude (which is the second float) might be between 0 and 180, optimistic or unfavourable.

Coordinates can solely comprise digits, or one of many following symbols (together with house after the comma)

There must be no house between the minus “-” signal and the digit after it.

Listed here are some legitimate coordinates:

  • -23, 25
  • 24.53525235, 23.45235
  • 04, -23.234235
  • 43.91343345, 143
  • 4, -3

And a few invalid ones:

  • 23.234, – 23.4234
  • 2342.43536, 34.324236
  • N23.43345, E32.6457
  • 99.234, 12.324
  • 6.325624, 43.34345.345
  • 0, 1,2
  • 0.342q0832, 1.2324

The answer in Golang

Possibility 1:

package deal resolution
import (
  "strconv"
  "strings"
)
func IsValidCoordinates(coordinates string) bool {
    if strings.Accommodates(coordinates, "e") {
        return false
    }
    coords := strings.Cut up(coordinates, ", ")
    coord1, err1 := strconv.ParseFloat(coords[0], 64)
    coord2, err2 := strconv.ParseFloat(coords[1], 64)
    if err1 != nil || err2 != nil || coord1 < -90 || coord1 > 90 || coord2 < -180 || coord2 > 180 {
        return false
    }   
    return true
}

Possibility 2:

package deal resolution
import (
  "math"
  "strings"
  "strconv"
)
func IsValidCoordinates(coordinates string) bool {
  var (
    err error
    coord []string
    num float64
  )
  if coord = strings.Cut up(coordinates, ", "); len(coord) != 2 {
    return false
  }
  for indx, c := vary coord {
    if strings.ContainsRune(c, 'e') { //verify for scientific notation
      return false
    }
    if num, err = strconv.ParseFloat(c, 64); err != nil {
      return false
    }
    if math.Abs(num) > float64(90*(indx+1)) { 
      return false
    }
  }
  return true
}

Possibility 3:

package deal resolution
import "regexp"
func IsValidCoordinates(coordinates string) bool [01]?[0-7]?d(.d+)?)$`)
  return regex.MatchString(coordinates)

Take a look at instances to validate our resolution

package deal solution_test
import (
  . "github.com/onsi/ginkgo"
  . "github.com/onsi/gomega"
)
var _ = Describe("Legitimate coordinates", func() {

	validCoordinates := []string{
		"-23, 25",
		"4, -3",
		"24.53525235, 23.45235",
		"04, -23.234235",
		"43.91343345, 143"}

	It("ought to validate the coordinates", func() {
		for _, coordinates := vary validCoordinates {
			Anticipate(IsValidCoordinates(coordinates)).To(Equal(true))
		}
	})
})

var _ = Describe("Invalid coordinates", func() {

	invalidCoordinates := []string{
		"23.234, - 23.4234",
		"2342.43536, 34.324236",
		"N23.43345, E32.6457",
		"99.234, 12.324",
		"6.325624, 43.34345.345",
		"0, 1,2",
		"0.342q0832, 1.2324",
		"23.245, 1e1"}
    
	It("ought to invalidate the coordinates", func() {
		for _, coordinates := vary invalidCoordinates {
			Anticipate(IsValidCoordinates(coordinates)).To(Equal(false))
		}
	})
})

About the author

admin

Leave a Comment