Structs

    Go

    1. package main
    2.  
    3. import (
    4. "fmt"
    5. "math"
    6. )
    7.  
    8. x float64
    9. y float64
    10. }
    11.  
    12. func distance(point1 Point, point2 Point) float64 {
    13. return math.Sqrt(point1.x*point2.x + point1.y*point2.y)
    14. }
    15.  
    16. // Since structs get automatically copied,
    17. func distance_better(point1 *Point, point2 *Point) float64 {
    18. return math.Sqrt(point1.x*point2.x + point1.y*point2.y)
    19. }
    20.  
    21. func main() {
    22. p1 := Point{1, 3}
    23. p2 := Point{2, 4}
    24. fmt.Println(distance(p1, p2)) // 3.7416573867739413
    25. fmt.Println(distance_better(&p1, &p2)) // 3.7416573867739413