1

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

Zeke Lu
  • 6,349
  • 1
  • 17
  • 23
lanyyyy
  • 11
  • 1
  • I've tried to tidy up your question but am not clear on what specific problem you face. [This](https://go.dev/play/p/HqcPdmsBYZr) might provide a starting point (at least it will allow you to provide a working example). – Brits May 17 '23 at 03:40
  • 4
    ` {{getStaticStr "user_should_share_the_information" }}` – Burak Serdar May 17 '23 at 03:45
  • Thanks for Burak Serdar, you are right: {{getStaticStr \"user_should_share_the_information\" }} – lanyyyy May 17 '23 at 06:24

0 Answers0