2020年2月3日月曜日

開発環境

入門Goプログラミング (Nathan Youngman(著)、Roger Peppé(著)、吉川 邦夫(監修, 翻訳)、翔泳社)のUNIT 5(状態と振る舞い)、LESSON 21(構造体)の練習問題の解答を求めてみる。

コード

package main

import (
    "encoding/json"
    "fmt"
    "os"
)

type location struct {
    Name string  `json:"name"`
    Lat  float64 `json:"latitude"`
    Long float64 `json:"longitude"`
}

func main() {
    locations := []location{
        {Name: "Bradbury Landing", Lat: -4.5895, Long: 137.4417},
        {Name: "Columbia memorial Station", Lat: -14.5684, Long: 175.4726366},
        {Name: "Challenger Memorial Station", Lat: -1.9462, Long: 354.4734},
    }
    byte, err := json.MarshalIndent(locations, ">", "    ")
    if err != nil {
        fmt.Fprint(os.Stderr, err)
        os.Exit(1)
    }
    fmt.Println(string(byte))
}

入出力結果(Zsh、PowerShell、Terminal)

% go run ./landing.go
[
>    {
>        "name": "Bradbury Landing",
>        "latitude": -4.5895,
>        "longitude": 137.4417
>    },
>    {
>        "name": "Columbia memorial Station",
>        "latitude": -14.5684,
>        "longitude": 175.4726366
>    },
>    {
>        "name": "Challenger Memorial Station",
>        "latitude": -1.9462,
>        "longitude": 354.4734
>    }
>]
%

0 コメント:

コメントを投稿