【发布时间】:2019-09-29 04:04:19
【问题描述】:
我想知道是否有可能以及如何测试一个值是否是 Go 模板中的字符串。
我尝试了以下方法,但没有成功
{{- range .Table.PrimaryKeys.DBNames.Sorted }}{{ with (index $colsByName .)}}
{{ .Name }}: {{ if .IsArray }}[]{{ end }}'{{.Type}}', {{end}}
{{- end }}
{{- range $nonPKDBNames }}{{ with (index $colsByName .) }}
{{ .Name }}: {{ if .IsArray }}[]{{end -}} {
type: {{ if .Type IsString}}GraphQLString{{end -}}, # line of interest where Type is a value that could be a number, string or an array
}, {{end}}
{{- end }}
这是我得到的错误
错误:解析 TablePaths 时出错:解析内容模板时出错:模板:templates/table.gotmpl:42:未定义函数“IsString”
【问题讨论】:
-
你想测试什么
.Type?请出示minimal reproducible example。 -
这并不重要,因为此示例中的
Type只是一个值。 -
这很重要,因为从它的名称来看,它看起来你将类型存储为
reflect.Type之类的东西,这将改变实现你想要的方式。
标签: string go types go-templates