typestar

Struct with method in Go

A struct with a method, and the value-versus-pointer receiver choice.

type Point struct {
    X, Y float64
}

func (p Point) DistanceTo(q Point) float64 {
    dx := p.X - q.X
    dy := p.Y - q.Y
    return math.Sqrt(dx*dx + dy*dy)
}

Keywords and builtins used here

The run, in numbers

Lines
9
Characters to type
146
Tokens
53
Three-star pace
95 tpm

At the three-star pace of 95 tokens a minute, this run takes about 33 seconds.

Type this snippet

Step 2 of 3 in Structs & methods, step 2 of 19 in Interfaces & generics.

← Previous Next →