如何使用特定键访问该结构映射中的结构字段值
How to access struct field value in a map of that struct with a specific key
我有 "FormError" 结构。我将这个结构传递给我的模板。那么如何使用模板中的特定键访问 InputError 结构字段值?
type InputError struct {
Val string
Has bool
}
type FormError struct {
Errs map[string]InputError
}
这行不通。
<input name="Name" type="text" value="{{index .Errs.Val `Name`}}">
Errs.Val
无效,您需要将查找和字段访问分开:
{{ $myval := index .Errs "key" }} {{ $myval.Val }}
或者如果您只需要使用该值一次:
{{ (index .Errs "key").Val }}
使用{{.Errs.Name.Val}}
。不需要使用索引。
我有 "FormError" 结构。我将这个结构传递给我的模板。那么如何使用模板中的特定键访问 InputError 结构字段值?
type InputError struct {
Val string
Has bool
}
type FormError struct {
Errs map[string]InputError
}
这行不通。
<input name="Name" type="text" value="{{index .Errs.Val `Name`}}">
Errs.Val
无效,您需要将查找和字段访问分开:
{{ $myval := index .Errs "key" }} {{ $myval.Val }}
或者如果您只需要使用该值一次:
{{ (index .Errs "key").Val }}
使用{{.Errs.Name.Val}}
。不需要使用索引。