開発環境
- macOS Catalina - Apple (OS)
- Emacs (Text Editor)
- Windows 10 Pro (OS)
- Visual Studio Code (Text Editor)
- Go (プログラミング言語)
入門Goプログラミング (Nathan Youngman(著)、Roger Peppé(著)、吉川 邦夫(監修, 翻訳)、翔泳社)のUNIT 5(状態と振る舞い)、LESSON 22(Goにはクラスがないけれど)の練習問題-1の解答を求めてみる。
コード
package main
import "fmt"
type coordinate struct {
d, m, s float64
h rune
}
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)
}
type location struct {
lat, long float64
}
func newLocation(lat, long coordinate) location {
return location{lat.decimal(), long.decimal()}
}
func main() {
locations := []location{
newLocation(
coordinate{14, 34, 6.2, 'S'},
coordinate{175, 28, 21.5, 'E'},
),
newLocation(
coordinate{1, 56, 46.3, 'w'},
coordinate{354, 28, 24.2, 'e'},
),
newLocation(
coordinate{4, 35, 22.2, 's'},
coordinate{137, 26, 30.1, 'e'},
),
newLocation(
coordinate{4, 30, 0, 'n'},
coordinate{135, 54, 0, 'e'},
),
}
for _, location := range locations {
fmt.Printf("%v\n%+[1]v\n", location)
}
}
入出力結果(Zsh、PowerShell、Terminal)
% go run ./landing.go
{-14.568388888888888 175.4726388888889}
{lat:-14.568388888888888 long:175.4726388888889}
{-1.9461944444444446 354.47338888888885}
{lat:-1.9461944444444446 long:354.47338888888885}
{-4.5895 137.44169444444444}
{lat:-4.5895 long:137.44169444444444}
{4.5 135.9}
{lat:4.5 long:135.9}
%
0 コメント:
コメントを投稿