golang 模板中字符串切片的范围

问题描述 投票:0回答:3

我有一个结构,其中包含如下所示的一段字符串类型。

 type Data struct {
      DataFields []string
 }

在我的 html 模板文件中,我想遍历字符串切片。但是,各个字段只是没有任何结构名称的字符串。我如何遍历包含简单类型(如字符串、整数等)的切片?

go go-templates
3个回答
35
投票

使用

.
来引用简单的值,如字符串、整数等。

 {{range .DataFields}}{{.}}{{end}}

在操场上运行它.

您也可以像

{{range $v := .DataFields}}{{$v}}{{end}}
那样分配给模板变量,但这是额外的工作。拥抱
.
.


31
投票

或者将它赋给一个变量,类似于普通的 Go range 子句:

 {{range $element := .DataFields}} {{$element}} {{end}}

在操场上运行它

来自 docs for text/template(用作 html/template 的接口文档):

{{范围管道}} T1 {{结束}}
    管道的值必须是数组、切片、映射或通道。
    如果管道的值的长度为零,则不输出任何内容;
    否则,dot 被设置为数组的连续元素,
    slice 或 map 并执行 T1。如果值是一个映射并且
    键是具有定义顺序(“可比较”)的基本类型,
    元素将按排序的键顺序访问。

...

action 中的管道可以初始化变量以捕获结果。初始化有语法

$variable := pipeline

...

如果“范围”操作初始化了一个变量,则该变量被设置为迭代的连续元素。此外,“范围”可以声明两个变量,用逗号分隔:

range $index, $element := pipeline

在这种情况下,$index 和 $element 分别设置为数组/切片索引或映射键和元素的连续值。 注意,如果只有一个变量,则赋值为元素;这与 Go 范围子句中的约定相反。

(加粗部分是我强调的)


0
投票

我在 .tpl 文件中定义了一些模板,但在使用 range 函数后,我无法通过 include 引用我的模板。

_helpers.tpl


{{/*
Expand the name of the chart.
*/}}
{{- define "kong.name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }}
{{- end }}

{{/*
Create a default fully qualified app name.
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
If release name contains chart name it will be used as a full name.
*/}}
{{- define "kong.fullname" -}}
{{- if .Values.fullnameOverride }}
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- $name := default .Chart.Name .Values.nameOverride }}
{{- if contains $name .Release.Name }}
{{- .Release.Name | trunc 63 | trimSuffix "-" }}
{{- else }}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }}
{{- end }}
{{- end }}
{{- end }}

{{/*
Create chart name and version as used by the chart label.
*/}}
{{- define "kong.chart" -}}
{{- printf "%s-%s" $.Chart.Name $.Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}
{{- end }}

{{/*
Common labels
*/}}
{{- define "kong.labels" -}}
helm.sh/chart: {{ include "kong.chart" . }}
{{ include "kong.selectorLabels" . }}
{{- if $.Chart.AppVersion }}
app.kubernetes.io/version: {{ $.Chart.AppVersion | quote }}
{{- end }}
app.kubernetes.io/managed-by: {{ $.Release.Service }}
{{- end }}

{{/*
Selector labels
*/}}
{{- define "kong.selectorLabels" -}}
app.kubernetes.io/name: {{ include "kong.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- end }}

{{/*
Create the name of the service account to use
*/}}
{{- define "kong.serviceAccountName" -}}
{{- if .Values.serviceAccount.create }}
{{- default (include "kong.fullname" .) .Values.serviceAccount.name }}
{{- else }}
{{- default "default" .Values.serviceAccount.name }}
{{- end }}
{{- end }}

yaml文件:

{{- range .Values.kongEnvs }}
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: kong{{ .id }}-conf
{{/*  labels:*/}}
{{/*    helm.sh/chart: {{ printf "%s-%s" $.Chart.Name $.Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }}*/}}
{{/*    app.kubernetes.io/name: {{ default $.Chart.Name $.Values.nameOverride | trunc 63 | trimSuffix "-" }}*/}}
{{/*    app.kubernetes.io/instance: {{ $.Release.Name }}*/}}
{{/*    app.kubernetes.io/version: {{ $.Chart.AppVersion | quote }}*/}}
{{/*    app.kubernetes.io/managed-by: {{ $.Release.Service }}*/}}
  labels:
    {{- include "kong.labels" . | nindent 4 }}
data:
  kong.conf: |-
    mem_cache_size = 512m
{{- end }}

使用helm template命令出现如下错误信息:


$ helm template  kong . --debug                                                                                                                                                                                                                                           [23/03/29| 8:38PM]
install.go:173: [debug] Original chart version: ""
install.go:190: [debug] CHART PATH: /helm/kong


Error: template: kong/templates/configmap.yaml:14:8: executing "kong/templates/configmap.yaml" at <include "kong.labels" .>: error calling include: template: kong/templates/_helpers.tpl:37:18: executing "kong.labels" at <include "kong.chart" .>: error calling include: template: kong/templates/_helpers.tpl:30:20: executing "kong.chart" at <$.Chart.Name>: nil pointer evaluating interface {}.Name
helm.go:88: [debug] template: kong/templates/configmap.yaml:14:8: executing "kong/templates/configmap.yaml" at <include "kong.labels" .>: error calling include: template: kong/templates/_helpers.tpl:37:18: executing "kong.labels" at <include "kong.chart" .>: error calling include: template: kong/templates/_helpers.tpl:30:20: executing "kong.chart" at <$.Chart.Name>: nil pointer evaluating interface {}.Name

© www.soinside.com 2019 - 2024. All rights reserved.