<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                自定義io.Writer渲染 (index.html同模板渲染終端index.html保持不變) 我們看下myTemplate.Execute源碼 ~~~ func (t *Template) Execute(wr io.Writer, data interface{}) error { if err := t.escape(); err != nil { return err } return t.text.Execute(wr, data) } type Writer interface { Write(p []byte) (n int, err error) } ~~~ 所以我們實現Writer接口方法Write就可以自定義了 ~~~ package main import ( // "os" "fmt" "html/template" "io" "net/http" ) var myTemplate *template.Template type Person struct { Name string age string } type Result struct { output string } /* 自定義實現接口 type Writer interface { Write(p []byte) (n int, err error) } */ func (p *Result) Write(b []byte) (n int, err error) { fmt.Println("called by template") p.output += string(b) p.output += "*" return len(b), nil } /* func WriteString(w Writer, s string) (n int, err error) { if sw, ok := w.(stringWriter); ok { return sw.WriteString(s) } return w.Write([]byte(s)) } */ func userInfo(w http.ResponseWriter,r *http.Request) { p := Person{Name:"Murphy",age:"28"} resultWriter := &Result{} io.WriteString(resultWriter, "hello world\r\n") myTemplate.Execute(resultWriter,p) fmt.Println("render data:",resultWriter.output) } func initTemplate(fileName string) (err error){ myTemplate,err = template.ParseFiles(fileName) if err != nil{ fmt.Println("parse file err:",err) return } return } /* func HandleFunc(pattern string, handler func(ResponseWriter, *Request)) { DefaultServeMux.HandleFunc(pattern, handler) } */ func main() { initTemplate("./index.html") http.HandleFunc("/user/info", userInfo) err := http.ListenAndServe("0.0.0.0:8880", nil) if err != nil { fmt.Println("http listen failed") } } ~~~ 在瀏覽器中輸入: http://localhost:8880/user/info 然后在終端就會渲染出如下內容: $ go run main.go ~~~ called by template called by template called by template called by template called by template called by template render data: hello world *<html> <head> <title> </title> </head> <body> <p> hello,*Murphy* *{Murphy 28}* </p> </body> </html>* ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看