go-templates

It takes too much time when using “template” package to generate a dynamic web page to client in Golang

∥☆過路亽.° 提交于 2019-12-17 02:32:02
问题 It is so slow when using template package to generate a dynamic web page to client. Testing code as below, golang 1.4.1 http.Handle("/js/", (http.FileServer(http.Dir(webpath)))) http.Handle("/css/", (http.FileServer(http.Dir(webpath)))) http.Handle("/img/", (http.FileServer(http.Dir(webpath)))) http.HandleFunc("/test", TestHandler) func TestHandler(w http.ResponseWriter, r *http.Request) { Log.Info("Entering TestHandler ...") r.ParseForm() filename := NiConfig.webpath + "/test.html" t, err :=

Prometheus Slack alert with FAQ url

帅比萌擦擦* 提交于 2019-12-13 05:42:19
问题 I have a Prometheus alert like this: - alert: NginxCrashLoop annotations: description: Nginx at {{ $labels.pod }} is in Crash Loop identifier: '{{ $labels.node }}' runbook_url: https://sites.google.com/a/domain.com/bi/faq/nginx expr: | rate(kube_pod_container_status_restarts_total{container="frontend"}[2m]) * on(namespace, pod) group_left(node) kube_pod_info * 3600 > 3 labels: severity: critical and a Go template for Slack custom alerting: {{ define "__single_message_title" }}{{ range .Alerts

Apply custom html tag to gin-gonic response

女生的网名这么多〃 提交于 2019-12-11 14:32:08
问题 I'm trying to prettify the response printed trough gin-gonic . From a go POV, i populate a struct with a string. My scope is to apply html tag to the input string. // Struct for prettify response type DocResponse struct { Filename string `json:"filename"` Url string `json:"url"` Content string `json:"data"` } var response []DocResponse // iterating data for populate struct for i := range found { //NOTE: Apply a <div> tag tmp := DocResponse{Filename: found[i], Url: `<div>http://127.0.0.1:8080

The Go Template ParseFiles func parsing multiple files

强颜欢笑 提交于 2019-12-11 08:03:25
问题 What would it happen if I pass two or more files to Go Template's ParseFiles func? func (*Template) ParseFiles It helps says: ParseFiles parses the named files and associates the resulting templates with t. If an error occurs, parsing stops and the returned template is nil; otherwise it is t. There must be at least one file. Since the templates created by ParseFiles are named by the base names of the argument files, t should usually have the name of one of the (base) names of the files. If it

Telling Golang which template to execute first

自闭症网瘾萝莉.ら 提交于 2019-12-11 05:22:18
问题 I have a folder with different templates in golang. The main template is main.html and there is also a footer.html and header.html . Footer and Header are loaded with {{template "footer.html" .}} in main.html . I am using this to parse the files templates, _ := template.ParseGlob("Templates/" + template_name + "/*.html") because there are other directories with different file names used aswell. So I don't want to use parseFiles . However, the template that is displayed is always the first one

Docker swarm tries to parse the value of ENV variable in my compose file (because it has a go template in it) and gives me an error

时光怂恿深爱的人放手 提交于 2019-12-11 01:29:52
问题 The error I try to launch a logspout container and set the log format (an ENV variable) via a docker-compose file. Not too difficult, and if I launch it with docker-compose up , everything works fine. But when I try to launch it with docker swarm init and docker stack deploy -c docker-compose.yml mystack , I get an error: Error response from daemon: rpc error: code = InvalidArgument desc = expanding env failed: expanding env "RAW_FORMAT={ \"container\" : \"{{ .Container.Name }}\", \"labels\":

Compare two variables inside Go template

南楼画角 提交于 2019-12-10 16:44:33
问题 In the data I pass to my template I have the two variables Type and Res.Type I want to compare to preselect an option for my select field. To illustrate my problem I have created this simplified version: package main import ( "bufio" "bytes" "html/template" "log" ) type Result struct{ Type string } func main() { types := map[string]string{ "FindAllString": "FindAllString", "FindString": "FindString", "FindStringSubmatch": "FindStringSubmatch", } res := &Result{Type: "findAllString"}

Iterate Go map get index

做~自己de王妃 提交于 2019-12-10 15:45:13
问题 In order to use revel 's even keyword in templates I would like to get the index of a map entry when iterating with range . Is there any way to do so? My map has the structure: map[string][]string 回答1: You can't do this only with template actions, but you may register a function which provides the necessary help. You may register a function which returns a function (closure), which alternates its return value whenever called (exactly how "odd" and "even" indices alternate): func isEven() func

How to pass dynamic arguments to a helm chart that runs a job

纵然是瞬间 提交于 2019-12-10 13:09:07
问题 I'd like to allow our developers to pass dynamic arguments to a helm template (Kubernetes job). Currently my arguments in the helm template are somewhat static (apart from certain values) and look like this Args: --arg1 value1 --arg2 value2 --sql-cmd select * from db If I were run a task using the docker container without Kubernetes, I would pass parameters like so: docker run my-image --arg1 value1 --arg2 value2 --sql-cmd "select * from db" Is there any way to templatize arguments in a helm

How to write template output to a file in Golang?

孤街浪徒 提交于 2019-12-10 11:19:12
问题 I use the following code which work ok, but now I want to print the template to a file and tried the following but got error package main import ( "html/template" "log" "os" ) func main() { t := template.Must(template.New("").Parse(`{{- range .}}{{.}}: echo "from {{.}}" {{end}} `)) t.Execute(os.Stdout, []string{"app1", "app2", "app3"}) f, err := os.Create("./myfile") if err != nil { log.Println("create file: ", err) return } err = t.Execute(f, t) if err != nil { log.Print("execute: ", err)