Quick introduction to go lang
Go language is a language created (and released) by google. It is based on many existing "things" that got compiled to a target environment, where really many environments are available. Code is executed in some kind of a sandbox (similar to java runtime environment). You could also run the code without storing compiled binary usingPros: supports multiple environments, is "managed" (garbage collector), has good support, is simple, has built-in testing framework, is compiled into a binary, can be compiled on runtime
Cons: different from most languages, extensible only to some extend (at some point you need to get into low level sources), has "different" source management/versioning, testing package is pretty "raw",large binary footprint (simple "hello world" app would be probably 10MB large or 5MB after stripping)
Source code before
Note the error handling is omitted.
package main
import (
"html/template"
"net/http"
)
func handler(w http.ResponseWriter, r *http.Request) {
data := []map[string]string{{"k": "2", "v": "a"}, {"k": "1", "v": "b"}}
t, _ := template.ParseFiles("template.tmpl")
t.Execute(w, data)
}
func main() {
http.HandleFunc("/", handler)
http.ListenAndServe(":8081", nil)
}
<html><body>
{{range .}}
{{.v}},
{{end}}
</body></html>
Output: a,b,Quick solution
Source code after
Note the error handling is omitted.
package main
import (
"html/template"
"net/http"
)
func mySort(s []map[string]string
) map[string]map
[string]string
{
m := make(map[string]map
[string]string
, len(s))
for i:=0; i
= s[i]
m[s[i]["k"]]
}
return m
}
func handler(w http.ResponseWriter, r *http.Request) {
data := []map[string]string{{"k": "2", "v": "a"}, {"k": "1", "v": "b"}}
t, _ := template.New("template.tmpl").Funcs(template.FuncMap{"mySort":mySort}).ParseFiles("template.tmpl")
// Note that I cannot simply use ParseFiles as it calls New() inside
t.Execute(w, data)
}
func main() {
http.HandleFunc("/", handler)
http.ListenAndServe(":8081", nil)
}
<html><body>
{{range .|mySort}}
{{.v}},
{{end}}
</body></html>
Output: b,a,
No comments:
Post a Comment