# Golang : How to calculate the distance between two coordinates using Haversine formula

Tags : golang earth-radius distance haversine coordinates

I need to develop a service that calculate the distances of my current location to several points nearby. Each of the locations are represented by coordinates and my service will pick the 3 nearest points and select one of them. So, how to calculate the distances of my current location to each and every points?

Below is a simple example using the Haversine formula to calculate the distance between 2 coordinates.

Here you go!

```
package main
import (
"fmt"
"math"
)
type Coordinates struct {
Latitude float64
Longitude float64
}
const radius = 6371 // Earth's mean radius in kilometers
func degrees2radians(degrees float64) float64 {
return degrees * math.Pi / 180
}
func (origin Coordinates) Distance(destination Coordinates) float64 {
degreesLat := degrees2radians(destination.Latitude - origin.Latitude)
degreesLong := degrees2radians(destination.Longitude - origin.Longitude)
a := (math.Sin(degreesLat/2)*math.Sin(degreesLat/2) +
math.Cos(degrees2radians(origin.Latitude))*
math.Cos(degrees2radians(destination.Latitude))*math.Sin(degreesLong/2)*
math.Sin(degreesLong/2))
c := 2 * math.Atan2(math.Sqrt(a), math.Sqrt(1-a))
d := radius * c
return d
}
func main() {
pointA := Coordinates{2.990353, 101.533913}
pointB := Coordinates{2.960148, 101.577888}
fmt.Println("Point A : ", pointA)
fmt.Println("Point B : ", pointB)
distance := pointA.Distance(pointB)
fmt.Printf("The distance from point A to point B is %.2f kilometers.\n", distance)
}
```

Sample output:

Point A : {2.990353 101.533913}

Point B : {2.960148 101.577888}

The distance from point A to point B is 5.93 kilometers.

References :

#### See also : Golang : Find location by IP address and display with Google Map

Tags : golang earth-radius distance haversine coordinates

##### By Adam Ng(黃武俊)

IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.

#### Advertisement

#### Tutorials

+8.8k Golang : Example for DSA(Digital Signature Algorithm) package functions

+8.1k Mac/Linux and Golang : Fix bind: address already in use error

+5.3k Golang : Check whether a network interface is up on your machine

233 Golang : How to search a list of records or data structures

+3.1k Golang : Gorrila mux.Vars() function example

+5k Golang : Get uploaded file name or access uploaded files

+4.1k Golang : Sort and reverse sort a slice of floats

+3.3k Golang : Launch Mac OS X Preview (or other OS) application from your program example

+5.3k Golang : Exit, terminating or aborting a program

+4.4k Golang : Convert file unix timestamp to UTC time example

+1.5k Javascript : How to show different content with noscript?

+4.4k Golang : Search and extract certain XML data example