以下為 HTML程式碼,請新建 index.html 檔案,複製下面程式碼後,再把它放在專案目錄下

<html>
    <body>
        Hello {{.Name}} !!
    </body>
</html>

---------------------------------------------------------------------
以下為 Go 語言 程式碼:

package main

import (
    "html/template"
    "net/http"
)

type Person struct {
    Name string // 命名要大寫開頭
}

func indexHandle(w http.ResponseWriter, r *http.Request) {
    t,_ := template.ParseFiles("index.html") // 讀取 HTML 檔案
    p := Person{Name:"Loli"} // 要送到前端的訊息
    t.Execute(w, p)
}

func main() {
    http.HandleFunc("/", indexHandle) // 當瀏覽器輸入根目錄時會呼叫 indexHandle() 涵式
    http.ListenAndServe(":80", nil)
}

---------------------------------------------------------------------

輸出結果:

1  








arrow
arrow
    全站熱搜

    黃彥霖 發表在 痞客邦 留言(0) 人氣()