Skip to content

Geographic coordinates and distances library written in Go

License

Notifications You must be signed in to change notification settings

connerdouglass/go-geo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-geo

Go Reference

Geographic coordinates and distances library written in Go.

Representing coordinates

myLocation := geo.Coordinate{
    Latitude: 39.7684426,
    Longitude: -86.1583112,
}

Calculating distances

a := geo.Coordinate{
    Latitude: 39.7684426,
    Longitude: -86.1583112,
}
b := geo.Coordinate{
    Latitude: 38.3531417,
    Longitude: -81.6388193,
}

// Two ways of calculating the distance:
distance := a.DistanceTo(b)
distance := geo.DistanceBetween(a, b)

fmt.Println("Distance is: ", distance)

// Prints:
// --> 420.6896449044601km

(Note: Ending up with a 420.69km example was a complete accident but I'm not changing it)

Working with geo.Distance

The geo.Distance type represents distances in geographical space. Some predefined constants can be used for convenience:

geo.Millimeter
geo.Centimeter
geo.Meter
geo.Kilometer
geo.Mile

You can construct distances like so:

twoMiles := 2 * geo.Mile
tenKm := 10 * geo.Kilometer

You can also fetch the float64 conversion of a distance represented in any units you like:

// Construct a distance (type geo.Distance)
distance := 15 * geo.Mile

// Get the equivalent number of kilometers (type float64)
inKilometers := distance.Kilometers()

In the above example, the value of inKilometers is equal to 24.14015969842181, which is the number of kilometers equal to 15 miles.