问题描述
对 Go 和使用范围非常陌生。我正在尝试使用范围中的值生成变量名称,并希望获取动态生成的变量的值。这是我的代码
<h1>
Best Basketball Shoes 2021 – Top 10 Basketball
Shoes To Have In 2021
</h1>
<p class="hide">NOVEMBER 13,2020 By IAN DANIEL</p>
当我运行上面的代码时,我得到以下
package main
import (
"log"
"os"
"text/template"
)
type EnvironmentCluster[] struct {
cluster string
env string
}
const tmpl = `
{{$application_configuration := "generic config"}}
{{$application_development_configuration := "dev_config"}}
{{$application_stage_configuration := "stage_config"}}
{{range $cluster,$env := .}}
{{$config := (printf "application_%s_configuration" $cluster) }}
Cluster is {{$cluster}},env is {{$env}},config is {{$config}}
{{end}}
`
func main() {
clusterEnv := map[string]string{
"development": "dev","stage": "prod",}
t := template.New("EnvironmentCluster template")
t,err := t.Parse(tmpl)
if err != nil {
log.Fatal("Parse: ",err)
return
}
err = t.Execute(os.Stdout,clusterEnv)
if err != nil {
log.Fatal("Execute: ",err)
return
}
}
但我的期望是看到 Cluster is development,env is dev,config is application_development_configuration
Cluster is stage,env is prod,config is application_stage_configuration
的实际值是这样打印的 -
config
任何帮助/指示?寻找在 go-templates 中访问要在范围内使用的范围外变量的方法。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)