佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > golang實現頁面靜態(tài)化操作的示例代碼

golang實現頁面靜態(tài)化操作的示例代碼

熱門標簽:網站排名優(yōu)化 呼叫中心市場需求 服務外包 百度競價排名 AI電銷 地方門戶網站 鐵路電話系統(tǒng) Linux服務器

什么是頁面靜態(tài)化:

簡單的說,我們如果訪問一個鏈接 ,服務器對應的模塊會處理這個請求,轉到對應的go方法,最后生成我們想要看到的數據。這其中的缺點是顯而易見的:因為每次請求服務器都會進行處理,如 果有太多的高并發(fā)請求,那么就會加重應用服務器的壓力,弄不好就把服務器 搞down 掉了。那么如何去避免呢?如果我們把請求后的結果保存成一個 html 文件,然后每次用戶都去訪問 ,這樣應用服務器的壓力不就減少了?

那么靜態(tài)頁面從哪里來呢?總不能讓我們每個頁面都手動處理吧?這里就牽涉到我們要講解的內容了,靜態(tài)頁面生成方案… 我們需要的是自動的生成靜態(tài)頁面,當用戶訪問 ,會自動生成html文件 ,然后顯示給用戶。

為了路由方便我用的gin框架但是不管用在什么框架上面都是一樣的

項目目錄:

project

-tem

--index.html

-main.go

main.go文件代碼:

package main

import (
  "fmt"
  "net/http"
  "os"
  "path/filepath"
  "text/template"

  "github.com/gin-gonic/gin"
)

type Product struct {
  Id  int64 `json:"id"` //字段一定要大寫不然各種問題
  Name string `json:"name"`
}

//模擬從數據庫查詢過來的消息
var allproduct []*Product = []*Product{
  {1, "蘋果手機"},
  {2, "蘋果電腦"},
  {3, "蘋果耳機"},
}
var (
  //生成的Html保存目錄
  htmlOutPath = "./tem"
  //靜態(tài)文件模版目錄
  templatePath = "./tem"
)

func main() {
  r := gin.Default()
  r.LoadHTMLGlob("tem/*")
  r.GET("/index", func(c *gin.Context) {
    GetGenerateHtml()
    c.HTML(http.StatusOK, "index.html", gin.H{"allproduct": allproduct})
  })
  r.GET("/index2", func(c *gin.Context) {
    c.HTML(http.StatusOK, "htmlindex.html", gin.H{})
  })
  r.Run()
}

//生成靜態(tài)文件的方法
func GetGenerateHtml() {
  //1.獲取模版
  contenstTmp, err := template.ParseFiles(filepath.Join(templatePath, "index.html"))
  if err != nil {
    fmt.Println("獲取模版文件失敗")
  }
  //2.獲取html生成路徑
  fileName := filepath.Join(htmlOutPath, "htmlindex.html")
  //4.生成靜態(tài)文件
  generateStaticHtml(contenstTmp, fileName, gin.H{"allproduct": allproduct})
}

//生成靜態(tài)文件
func generateStaticHtml(template *template.Template, fileName string, product map[string]interface{}) {
  //1.判斷靜態(tài)文件是否存在
  if exist(fileName) {
    err := os.Remove(fileName)
    if err != nil {
      fmt.Println("移除文件失敗")
    }
  }
  //2.生成靜態(tài)文件
  file, err := os.OpenFile(fileName, os.O_CREATE|os.O_WRONLY, os.ModePerm)
  if err != nil {
    fmt.Println("打開文件失敗")
  }
  defer file.Close()
  template.Execute(file, product)
}

//判斷文件是否存在
func exist(fileName string) bool {
  _, err := os.Stat(fileName)
  return err == nil || os.IsExist(err)
}

tem/index.html文件代碼:

{{define "index.html"}}
!DOCTYPE html>
html lang="en">
head>
  meta charset="UTF-8">
  title>商品列表頁/title>
/head>
tbody>

div>a href="#" rel="external nofollow" >商品列表頁/a>/div>
table border="1">
  thead>
  tr>
    th>ID/th>
    th>商品名稱/th>
  /tr>
  /thead>
  tbody>
  {{range .allproduct}}
  tr>
    td>{{.Id}}/td>
    td>{{.Name}}/td>
  /tr>
  {{end}}
  /tbody>
/table>
/html>
{{end}}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • golang搭建靜態(tài)web服務器的實現方法
  • golang實現http服務器處理靜態(tài)文件示例
  • golang一些常用的靜態(tài)檢查工具詳解

標簽:黃山 衡水 蘭州 湖南 湘潭 銅川 仙桃 崇左

巨人網絡通訊聲明:本文標題《golang實現頁面靜態(tài)化操作的示例代碼》,本文關鍵詞  ;如發(fā)現本文內容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內容系統(tǒng)采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    维西| 乐陵市| 运城市| 巴南区| 扶风县| 大城县| 孝义市| 石嘴山市| 高密市| 沁源县| 奇台县| 高淳县| 息烽县| 鸡泽县| 丰城市| 电白县| 林西县| 彭泽县| 河间市| 图木舒克市| 日土县| 锡林郭勒盟| 永川市| 纳雍县| 余干县| 汨罗市| 无棣县| 新闻| 离岛区| 同仁县| 湘阴县| 泗水县| 桦南县| 馆陶县| 三台县| 凌源市| 田林县| 社旗县| 江安县| 册亨县| 灵丘县|