How to pass static variable to golang text/template function

123 views Asked by At

I want to pass the static string "user_should_share_the_information" to this function, but the text/template tell me that the function "user_should_share_the_information" not found But I just only want to treat "user_should_share_the_information" as a static string

package main

import (
    "bytes"
    "fmt"
    "text/template"
)

type people struct {
    name string
    age  int
}

func getStaticStr(val string) string {
    // getTextFromUserCenterByKey is a function that call the remote server to get content text
    return getTextFromUserCenterByKey(val)
}

func main() {
    funcs := template.FuncMap{
        "getStaticStr": getStaticStr,
    }
    tmpl, err := template.New("info").Funcs(funcs).Parse(
        "info: {{getStaticStr user_should_share_the_information }}")
    if err != nil {
        fmt.Println(err)
        return
    }
    buffer := bytes.Buffer{}
    _ = tmpl.Execute(&buffer, map[string]interface{}{})
    fmt.Println(buffer.String())
}

My purpose is that I want to use template function to show different content, and the control variable is defined in the template, not in the data

0

There are 0 answers