【Go】構造体サンプルメモ

## サンプル

```go
package main

import "fmt"

type Person struct {
	name string
	age int
}

func main() {
	// 宣言の後、値を代入
	var person1 Person
	person1.name = "Akira"
	person1.age = 20
	fmt.Println(person1)

	// 宣言＋代入
	person2 := Person{age: 30, name: "Kure"}
	fmt.Println(person2)

	// 宣言＋代入
	person3 := Person{"Aki", 40}
	fmt.Println(person3)
}
```


```go
package main

import (
	"fmt"
	"encoding/json"
)

func main() {
	type location struct {
		Name string
		Lat float64
		Long float64
	}

	// JSON 変換するためにキーを大文字にしておく必要がある
	locations := []location {
		{Name: "Brandbury Landing", Lat: -4.5895, Long: 137.4417},
		{Name: "Columbia Memorial Station", Lat: -14.56684, Long: 175.472636},
		{Name: "Challenger Memorial Station", Lat: -1.9462, Long: 354.4734},
	}

	// [{Brandbury Landing -4.5895 137.4417} {Columbia Memorial Station -14.56684 175.472636} {Challenger Memorial Station -1.9462 354.4734}]
	fmt.Println(locations)

	b, _ := json.Marshal(locations)
	// [{"Name":"Brandbury Landing","Lat":-4.5895,"Long":137.4417},{"Name":"Columbia Memorial Station","Lat":-14.56684,"Long":175.472636},{"Name":"Challenger Memorial Station","Lat":-1.9462,"Long":354.4734}]
	fmt.Println(string(b))

	// JSONキーを指定
	type location2 struct {
		Name string `json:"name"`
		Lat float64 `json:"lat"`
		Long float64 `json:"long"`
	}

	locations2 := []location2 {
		{Name: "Brandbury Landing", Lat: -4.5895, Long: 137.4417},
		{Name: "Columbia Memorial Station", Lat: -14.56684, Long: 175.472636},
		{Name: "Challenger Memorial Station", Lat: -1.9462, Long: 354.4734},
	}

	// [{Brandbury Landing -4.5895 137.4417} {Columbia Memorial Station -14.56684 175.472636} {Challenger Memorial Station -1.9462 354.4734}]
	fmt.Println(locations2)

	c, _ := json.MarshalIndent(locations2, "", "  ")
  //	[
  //  {
  //    "name": "Brandbury Landing",
  //    "lat": -4.5895,
  //    "long": 137.4417
  //  },
  //  {
  //    "name": "Columbia Memorial Station",
  //    "lat": -14.56684,
  //    "long": 175.472636
  //  },
  //  {
  //    "name": "Challenger Memorial Station",
  //    "lat": -1.9462,
  //    "long": 354.4734
  //  }
  //]
	fmt.Println(string(c))
}
```

## 参考

<div class="amazlet-box" style="margin-bottom:0px;"><div class="amazlet-image" style="float:left;margin:0px 12px 1px 0px;"><a href="http://www.amazon.co.jp/exec/obidos/ASIN/4863541783/kuredev-22/ref=nosim/" name="amazletlink" target="_blank"><img src="https://images-fe.ssl-images-amazon.com/images/I/419XRomH%2BNL._SL160_.jpg" alt="改訂2版 基礎からわかる Go言語" style="border: none;" /></a></div><div class="amazlet-info" style="line-height:120%; margin-bottom: 10px"><div class="amazlet-name" style="margin-bottom:10px;line-height:120%"><a href="http://www.amazon.co.jp/exec/obidos/ASIN/4863541783/kuredev-22/ref=nosim/" name="amazletlink" target="_blank">改訂2版 基礎からわかる Go言語</a><div class="amazlet-powered-date" style="font-size:80%;margin-top:5px;line-height:120%">posted with <a href="http://www.amazlet.com/" title="amazlet" target="_blank">amazlet</a> at 20.02.15</div></div><div class="amazlet-detail">古川 昇 <br />シーアンドアール研究所 (2015-07-17)<br />売り上げランキング: 201,077<br /></div><div class="amazlet-sub-info" style="float: left;"><div class="amazlet-link" style="margin-top: 5px"><a href="http://www.amazon.co.jp/exec/obidos/ASIN/4863541783/kuredev-22/ref=nosim/" name="amazletlink" target="_blank">Amazon.co.jpで詳細を見る</a></div></div></div><div class="amazlet-footer" style="clear: left"></div></div>