How to Create a Coordinates Validator in Golang

The challenge

You need to create a function that will validate if given parameters are valid geographical coordinates.

Valid coordinates look like the following: “23.32353342, -32.543534534”. The return value should be either true or false.

Latitude (which is the first float) can be between 0 and 90, positive or negative. Longitude (which is the second float) can be between 0 and 180, positive or negative.

Coordinates can only contain digits, or one of the following symbols (including space after the comma)

There should be no space between the minus “-” sign and the digit after it.

Here are some valid coordinates:

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

And some 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 solution in Golang

Option 1:

package solution
import (
  "strconv"
  "strings"
)
func IsValidCoordinates(coordinates string) bool {
    if strings.Contains(coordinates, "e") {
        return false
    }
    coords := strings.Split(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
}

Option 2:

package solution
import (
  "math"
  "strings"
  "strconv"
)
func IsValidCoordinates(coordinates string) bool {
  var (
    err error
    coord []string
    num float64
  )
  if coord = strings.Split(coordinates, ", "); len(coord) != 2 {
    return false
  }
  for indx, c := range coord {
    if strings.ContainsRune(c, 'e') { //check 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
}

Option 3:

package solution
import "regexp"
func IsValidCoordinates(coordinates string) bool {
  regex := regexp.MustCompile(`^-?(90|[0-8]?\d(\.\d+)?), -?(180|[01]?[0-7]?\d(\.\d+)?)$`)
  return regex.MatchString(coordinates)
}

Test cases to validate our solution

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

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

	It("should validate the coordinates", func() {
		for _, coordinates := range validCoordinates {
			Expect(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("should invalidate the coordinates", func() {
		for _, coordinates := range invalidCoordinates {
			Expect(IsValidCoordinates(coordinates)).To(Equal(false))
		}
	})
})