golang template传递值的第二种方法 利用map[string]interface{}
2013-07-18 14:40
363 查看
关于golang tmplate怎么样传递值,以前的调查结果是只能用struct,但是最近开一个开源source,发现他竟然用的是map[string]interface{},百思不得其解,所以用一下代码进行验证。
PS,哎,golang的文档还是太弱了啊。
PS,哎,golang的文档还是太弱了啊。
package main import ( "fmt" "html/template" "net/http" "os" ) type Person struct { Name string Age int Emails []string Company string Role string } const templ = ` name is {{.name}} Company is {{.resources.Company}} ` func Handler(w http.ResponseWriter, r *http.Request) { chxd := Person{Company: "Intel", Name: "chxd"}} t := template.New("Person template") t, err := t.Parse(templ) checkError(err) var tmp map[string]interface{} = map[string]interface{}{"name":"macs", "resources":chxd} err = t.Execute(w, tmp) checkError(err) } func main() { http.HandleFunc("/", Handler) http.ListenAndServe(":8888", nil) } func checkError(err error) { if err != nil { fmt.Println("Fatal error ", err.Error()) os.Exit(1) } }
相关文章推荐
- GoLang 遍历 map、slice、array方法和template遍历map
- golang json 为map[string] interface{}
- Golang开发实践:把数据库数据保存到map[string]interface{}中
- JavaScript中利用Array和Object实现Map的方法
- XML文本转为MAP对象的简便方法(利用jackson-dataformat-xml)
- Java中利用MessageFormat对象实现类似C# string.Format方法格式化
- Java学习笔记(八):Map<stirng,string>的遍历的四种方法
- 利用Array Prototype的方法来实现对dom集合的筛选、indexOf、map等功能
- 利用Python命令行传递实例化对象的方法
- 利用js方法实现html静态页面间参数传递
- Map<String, String> 遍历的四种方法
- expected number,sequence,or string.map evaluated instead of freemarker.template.smplehash
- Map<String,Map<String,List<String>>>结构取数据这个方法比Iterator和foreach效率稍高。
- 利用onNewIntent方法传递参数
- map[string]Interface{}
- The constructor SimpleAdapter(new View.OnClickListener(){}, List<Map<String,Object>>, ..的解决方法
- map 遍历第二种方法 MapEntry
- 利用Python脚本生成sitemap.xml的实现方法
- 分页时,如何访问第一页即传递参数(第二种方法)
- Map<String, String>循环遍历的方法