開発環境
- macOS Catalina - Apple (OS)
- Emacs (Text Editor)
- Windows 10 Pro (OS)
- Visual Studio Code (Text Editor)
- Go (プログラミング言語)
Go Systems Programming: Master Linux and Unix system level programming with Go (Mihalis Tsoukalos(著)、Packt Publishing)のChapter 3(Advanced Go Features)、Exercises 11.の解答を求めてみる。
コード
package main
import (
"fmt"
"reflect"
)
type coordinate int
type car struct {
lat, long coordinate
name string
}
func main() {
c := car{lat: 2, long: 3, name: "Car"}
v := reflect.ValueOf(&c)
e := v.Elem()
t := e.Type()
fmt.Printf("ValueOf: %v\nElem: %v\nType: %v\n", v, e, t)
for i := 0; i < e.NumField(); i++ {
fmt.Printf("%v, Field Name: %v, Type: %v\n",
i, t.Field(i).Name, e.Field(i).Type())
}
}
入出力結果(Zsh、PowerShell、Terminal)
% go run main.go
ValueOf: &{2 3 Car}
Elem: {2 3 Car}
Type: main.car
0, Field Name: lat, Type: main.coordinate
1, Field Name: long, Type: main.coordinate
2, Field Name: name, Type: string
%
0 コメント:
コメントを投稿