Created
August 28, 2019 00:17
-
-
Save yurakawa/e119125f02a89bab71586bcebd2891d3 to your computer and use it in GitHub Desktop.
MarshalJSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://github.com/nathany/get-programming-with-go/blob/master/lesson24/starship/starship.go | |
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"os" | |
) | |
// coordinate in degrees, minutes, seconds in a N/S/E/W hemisphere. | |
type coordinate struct { | |
d, m, s float64 | |
h rune | |
} | |
// String formats a DMS coordinate. | |
func (c coordinate) String() string { | |
return fmt.Sprintf("%vº%v'%.1f\" %c", c.d, c.m, c.s, c.h) | |
} | |
// decimal converts a d/m/s coordinate to decimal degrees. | |
func (c coordinate) decimal() float64 { | |
sign := 1.0 | |
switch c.h { | |
case 'S', 'W', 's', 'w': | |
sign = -1 | |
} | |
return sign * (c.d + c.m/60 + c.s/3600) | |
} | |
func (c coordinate) MarshalJSON() ([]byte, error) { | |
return json.Marshal(struct { | |
DD float64 `json:"decimal"` | |
DMS string `json:"dms"` | |
D float64 `json:"degrees"` | |
M float64 `json:"minutes"` | |
S float64 `json:"seconds"` | |
H string `json:"hemisphere"` | |
}{ | |
DD: c.decimal(), | |
DMS: c.String(), | |
D: c.d, | |
M: c.m, | |
S: c.s, | |
H: string(c.h), | |
}) | |
} | |
// location with a latitude, longitude in decimal degrees. | |
type location struct { | |
Name string `json:"name"` | |
Lat coordinate `json:"latitude"` | |
Long coordinate `json:"longitude"` | |
} | |
func main() { | |
elysium := location{ | |
Name: "Elysium Planitia", | |
Lat: coordinate{4, 30, 0.0, 'N'}, | |
Long: coordinate{135, 54, 0, 'E'}, | |
} | |
bytes, err := json.MarshalIndent(elysium, "", " ") | |
if err != nil { | |
fmt.Println(err) | |
os.Exit(1) | |
} | |
fmt.Println(string(bytes)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment