How to print an object in Golang template like if we could have done in JavaScript

huangapple go评论66阅读模式
英文:

How to print an object in Golang template like if we could have done in JavaScript

问题

你可以使用fmt.Printf函数来打印整个对象。在Golang中,你可以使用%+v格式化动词来打印结构体的字段和值。以下是一个示例代码:

package main

import (
	"fmt"
)

type Query struct {
	Param1 string
	Param2 int
}

func main() {
	query := Query{
		Param1: "value1",
		Param2: 2,
	}

	fmt.Printf("%+v\n", query)
}

在上面的示例中,%+v将打印出Query结构体的字段和值。你可以根据自己的需要修改代码以适应你的情况。

英文:
renderTemplate(w, "index", map[string]interface{}{
    "ActualQAll": req.URL.Query(),
})

and inside golanghtml

...{{.ActualQAll}}...

But it shows nothing.

how can I print the whole object out like if I could do in JavaScript by doing JSON.stringify(obj) an object?

答案1

得分: 1

使用fmt.Sprintf函数,可能类似以下方式:

renderTemplate(w, "index", fmt.Sprintf("%s", ActuallQAll))

我不确定对象的具体样子。

英文:

Use fmt.Sprintf function, maybe something like below:

renderTemplate(w, "index", fmt.Sprintf("%s", ActuallQAll))

I'm not sure what exactly the object looks like.

huangapple
  • 本文由 发表于 2017年9月16日 05:42:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/46247825.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定