html不能接收到变量,为什么传递的变量不能在html / template中呈现?

我不知道为什么传递的变量没有用html / template呈现

这是在浏览器中呈现的内容:

这是所有传递的变量:[0xc0000a8ec0 0xc0000a8f80 0xc0000a9040 0xc0000a9100]

城市人口州国家首都

这是日志:

$ go运行main.go

2019/11/27 11:00:39 **** => &city has &main.City{Name:"Washington D.C.", State:"", Country:"USA", Capital:false, Population:680000} before appending to cities ****

2019/11/27 11:00:39 **** => &city has &main.City{Name:"Los Angeles", State:"CA", Country:"USA", Capital:false, Population:3900000} before appending to cities ****

2019/11/27 11:00:39 **** => &city has &main.City{Name:"San Francisco", State:"CA", Country:"USA", Capital:false, Population:860000} before appending to cities ****

2019/11/27 11:00:39 **** => &city has &main.City{Name:"Tokyo", State:"", Country:"Japan", Capital:true, Population:9000000} before appending to cities ****

**** => cities outside {} has 4

这是处理程序函数:

func indexHandler(w http.ResponseWriter, r *http.Request) {

projectID := "XXXXXXXXXXXXXX"

ctx := context.Background()

client, _ := firestore.NewClient(ctx, projectID)

query := client.Collection("cities").Documents(ctx)

defer query.Stop()

cities := make([]*City, 0)

for {

doc, err := query.Next()

if err == iterator.Done {

break

}

c := doc.Data()

// is there an easier way to populate the city struct

city := City{

Name: c["name"].(string),

Country: c["country"].(string),

Population: c["population"].(int64),

}

// This is my ugly solution to dealing with nil value from Firestore

_, ok := c["capital"]

if ok {

city.Capital = c["capital"].(bool)

}

state, ok := c["state"].(string)

if ok {

city.State = state

}

log.Printf("**** => &city has %#v before appending to cities ****", &city)

cities = append(cities, &city)

}

fmt.Printf("**** => cities outside {} has %d", len(cities))

indexTemplate.Execute(w, cities)

}

这是index.html模板:{% autoescape true %}

This is all the passed variables: {{ . }}

CityPopulation StateCountryCapital

{{ range .cities }}

testing testing testingtesting testing {{.Name}} {{ .Population}} {{ .State}}{{.Country}} {{.Capital}}

{{ end }}

{{ $cities := . }}

{% endautoescape %}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值