計算機科学のブログ

building storage - Structs - Anonymous struct fields

Head First GoJay McGavren(著)、O’Reilly Media)の Chapter 8(building storage - Structs)、p.260(Exercise)の解答を求めてみる。

コード

main.go

package main

import (
	"fmt"
	"geo/geo"
)

func main() {
	location := geo.Landmark{}
	location.Name = "THe Googleplex"
	location.Latitude = 37.42
	location.Longitude = -122.08
	fmt.Println(location)
}

geo/geo.go

package geo

type Coordinates struct {
	Latitude  float64
	Longitude float64
}
type Landmark struct {
	Name string
	Coordinates
}

入出力結果(Terminal, Zsh)

% go run ./main.go
{THe Googleplex {37.42 -122.08}}
% tree
.
├── geo
│   └── geo.go
├── go.mod
└── main.go

2 directories, 3 files
% cat go.mod 
module geo

go 1.23.4
%