35 lines
618 B
Go
35 lines
618 B
Go
package geo
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"fmt"
|
||
|
|
"strconv"
|
||
|
|
"strings"
|
||
|
|
)
|
||
|
|
|
||
|
|
type Coordinates struct {
|
||
|
|
Latitude float64
|
||
|
|
Longitude float64
|
||
|
|
}
|
||
|
|
|
||
|
|
func ParseCoordinates(s string) (*Coordinates, error) {
|
||
|
|
parts := strings.Split(s, ",")
|
||
|
|
if len(parts) != 2 {
|
||
|
|
return nil, fmt.Errorf("invalid string: %s", s)
|
||
|
|
}
|
||
|
|
|
||
|
|
var (
|
||
|
|
c = &Coordinates{}
|
||
|
|
err error
|
||
|
|
)
|
||
|
|
c.Latitude, err = strconv.ParseFloat(parts[0], 64)
|
||
|
|
if err != nil {
|
||
|
|
return nil, fmt.Errorf("failed to parse latitude: %v", err)
|
||
|
|
}
|
||
|
|
c.Longitude, err = strconv.ParseFloat(parts[1], 64)
|
||
|
|
if err != nil {
|
||
|
|
return nil, fmt.Errorf("failed to parse longitude: %v", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
return c, nil
|
||
|
|
}
|