src

Go monorepo.
git clone git://code.dwrz.net/src
Log | Files | Refs

geo.go (618B)


      1 package geo
      2 
      3 import (
      4 	"fmt"
      5 	"strconv"
      6 	"strings"
      7 )
      8 
      9 type Coordinates struct {
     10 	Latitude  float64
     11 	Longitude float64
     12 }
     13 
     14 func ParseCoordinates(s string) (*Coordinates, error) {
     15 	parts := strings.Split(s, ",")
     16 	if len(parts) != 2 {
     17 		return nil, fmt.Errorf("invalid string: %s", s)
     18 	}
     19 
     20 	var (
     21 		c   = &Coordinates{}
     22 		err error
     23 	)
     24 	c.Latitude, err = strconv.ParseFloat(parts[0], 64)
     25 	if err != nil {
     26 		return nil, fmt.Errorf("failed to parse latitude: %v", err)
     27 	}
     28 	c.Longitude, err = strconv.ParseFloat(parts[1], 64)
     29 	if err != nil {
     30 		return nil, fmt.Errorf("failed to parse longitude: %v", err)
     31 	}
     32 
     33 	return c, nil
     34 }