GoTemplateAndBootStrap3

Go模版与BootStrap3使用

Go模版看了老容易忘记,今天正好项目上工艺部门想要安全件的解析规则清单,我想着用.net开发还不如用Go模版+Bootstrap来个最佳实践得了.结果在公司多加班了半小时 : )

成品样图

image-20220307001641199
image-20220307001641199

实现了搜索功能 隔行变色 其他的表头排序还在研究中

文件结构

image-20220307001737794
image-20220307001737794

image-20220307001747021
image-20220307001747021

简单的 一个main.go 一个html(bootstrap.html没用 作为参考) 几张图片拼凑拼凑

实现步骤

  • 连接数据库 oracle 构造结构体完成数据库的数据映射问题

image-20220307002353869
image-20220307002353869

package main

import (
   "database/sql"
   "fmt"
   _ "github.com/mattn/go-oci8"
   "html/template"
   "log"
   "net/http"
   "os"
   "os/exec"
   "path/filepath"
   "time"
)

type Items struct {
   ROW_NUM       string
   PART_CODE     string
   PART_NAME     string
   PART_MODEL_NO sql.NullString
   PART_PLATFORM string
   PART_ABB      string
   RESERVE07     string
}

var DB *sql.DB

func DBconnect() {
   var err error
   //go连接mysql实例
   //数据库源信息
   dsn := "隐藏"
   //连接数据库
   DB, err = sql.Open("oci8", dsn) //不会校验用户名和密码是否正确
   if err != nil {
      log.Fatal(err)
   }
   err = DB.Ping()

   if err != nil {
      time.Sleep(5 * time.Minute)
      DBconnect()
   }
   return
}
  • 根据Go Template语法书写html代码

image-20220307002733907
image-20220307002733907

使用简单的 range . 和 .Row_num的迭代和.方法 并使用了bootstrap的link

image-20220307002825445
image-20220307002825445

  • 主函数解析模版 书写相应处理函数 回传数据完成渲染
image-20220307002922090
image-20220307002922090
t := template.Must(template.ParseFiles("template.html"))

// FileServer返回一个使用FileSystem接口root提供文件访问服务的HTTP处理器
file, _ := exec.LookPath(os.Args[0])
//得到全路径,比如在windows下E:\\golang\\test\\a.exe
path, _ := filepath.Abs(file)
rst := filepath.Dir(path)
http.Handle("/", http.FileServer(http.Dir(rst)))
  • 主函数书写处理函数 处理界面跳转 发布端口监听服务

image-20220307002539437
image-20220307002539437

这个函数完成检索数据 并渲染到界面上的功能 t就是模版

t := template.Must(template.ParseFiles("template.html"))
//检索所有
http.HandleFunc("/CheckPart", func(writer http.ResponseWriter, request *http.Request) {
   DBconnect()
   s := make([]Items, 0)
   if DB == nil {
      fmt.Fprintln(writer, "sry , please try again ...")
      return
   }
   rows, err2 := DB.Query("select ROWNUM, PART_CODE ,PART_NAME ,PART_MODEL_NO,PART_PLATFORM ,PART_ABB ,case RESERVE07 when '是' then '默认规则' when '否' then '不解析' else Reserve07 end RESERVE07 from part where PART_NAME like '%AQJ%'")
   if err2 != nil {
      fmt.Fprintln(writer, "sry , please try again ...")
      return
   }
   for rows.Next() {
      i := new(Items)
      rows.Scan(&i.ROW_NUM, &i.PART_CODE, &i.PART_NAME, &i.PART_MODEL_NO, &i.PART_PLATFORM, &i.PART_ABB, &i.RESERVE07)
      s = append(s, *i)
   }
   err2 = t.Execute(writer, s)
   if err2 != nil {
      panic(err2)
   }
})

//查询
http.HandleFunc("/Search", func(writer http.ResponseWriter, request *http.Request) {
		m := make(map[string]string)
		m["默认规则"] = "是"
		m["不解析"] = "否"
		request.ParseForm()
		value := request.FormValue("keyword")
		if v, ok := m[value]; ok {
			value = v
		}
		value = "%" + value + "%"
		DBconnect()
		s := make([]Items, 0)
		query, err := DB.Query("select ROWNUM, PART_CODE ,PART_NAME ,PART_MODEL_NO,PART_PLATFORM ,PART_ABB ,case RESERVE07 when '是' then '默认规则' when '否' then '不解析' else Reserve07 end RESERVE07 from part where PART_NAME like '%AQJ%' and (part_code like :1 or part_name like :2 or part_model_no like :3 or part_platform like :4 or part_abb like :5 or reserve07 like :6)", value, value, value, value, value, value)
		if err != nil {
			panic(err)
			return
		}
		for query.Next() {
			i := new(Items)
			query.Scan(&i.ROW_NUM, &i.PART_CODE, &i.PART_NAME, &i.PART_MODEL_NO, &i.PART_PLATFORM, &i.PART_ABB, &i.RESERVE07)
			s = append(s, *i)
		}
		err2 := t.Execute(writer, s)
		if err2 != nil {
			panic(err2)
		}
	})

//监听端口 发布服务
server := http.Server{
		Addr:              ":8081",
		Handler:           nil,
		TLSConfig:         nil,
		ReadTimeout:       0,
		ReadHeaderTimeout: 0,
		WriteTimeout:      0,
		IdleTimeout:       0,
		MaxHeaderBytes:    0,
		TLSNextProto:      nil,
		ConnState:         nil,
		ErrorLog:          nil,
		BaseContext:       nil,
		ConnContext:       nil,
	}
	log.Println(server.ListenAndServe())
Licensed under CC BY-NC-SA 4.0
Built with Hugo
Theme Stack designed by Jimmy