find the distance between two points in go

You can find the distance between two points in Go using the Pythagorean theorem. Here's an example that computes the distance between two points in a 2D plane:

main.go
package main

import (
	"fmt"
	"math"
)

type Point struct {
	X float64
	Y float64
}

func Distance(p, q Point) float64 {
	return math.Sqrt(math.Pow(p.X-q.X, 2) + math.Pow(p.Y-q.Y, 2))
}

func main() {
	p := Point{1, 2}
	q := Point{4, 6}
	fmt.Printf("Distance between %v and %v: %v\n", p, q, Distance(p, q))
	
	r := Point{-1, -5}
	s := Point{5, 1}
	fmt.Printf("Distance between %v and %v: %v\n", r, s, Distance(r, s))
}
420 chars
26 lines

This code defines a Point struct with x and y coordinates, and a Distance function that takes two points as input and returns their distance as a float64. The main function demonstrates how to use the Distance function with two different pairs of points.

gistlibby LogSnag