実現したいこと
Golangでstruct型から型の抽出をする方法を探しています。
typescriptだと以下のような記法です
typescript
1type TypeUser = { 2 name: string; 3 age: number; 4} 5 6const testString: TypeUser["name"] = "helloworld" 7const testNumber: TypeUser["age"] = 10
不可能であれば不可能と回答していただけると助かります
試したこと
refrect.Type型としての抽出は出来るのですがこれを型定義に使う方法が見つかりません
go
1package main 2 3import ( 4 "fmt" 5 "reflect" 6) 7 8func main() { 9 10type TUser struct { 11 name string 12 age int 13} 14 15TUserName := reflect.TypeOf(Tstruct{}.name) 16TUserAge := reflect.TypeOf(Tstruct{}.age) 17 18fmt.Println(TUserName) 19fmt.Println(TUserAge) 20}
string int
go
1package main 2 3import ( 4 "fmt" 5 "reflect" 6) 7 8func main() { 9 10type TUser struct { 11 name string 12 age int 13} 14 15TUserName := reflect.TypeOf(Tstruct{}.name) 16TUserAge := reflect.TypeOf(Tstruct{}.age) 17 18var testString TUserName 19var testInt TUserAge 20} 21
error_message
補足情報(FW/ツールのバージョンなど)
go 1.18
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。