想要在web上顯示一個表格,把各比值顯示出來,各位大神求解
<a href="/user/didadi" title="@didadi">@didadi</a> 想請教一下,整體的流程嗎?
希望能有個較完整程式碼
感謝~
#4
更多评论
package main
import (
//"html/template"
//"strings"
"log"
"net/http"
"fmt"
"database/sql"
_ "github.com/go-sql-driver/mysql"
)
var (
t , i int
array[]string
v string
c map[int]data
base []data = make([]data,0)
)
type data struct{
uid string
username string
departname string
created string
}
func main() {
fmt.Println("connect success")
db, err := sql.Open("mysql", "root:12345@tcp(127.0.0.1:3306)/123")//用户:密码@主机/数据库
checkErr(err)
defer db.Close()
i=0
rows, err := db.Query("SELECT * FROM userinfo")
checkErr(err)
for rows.Next() {
var uid, username, department, created string
err = rows.Scan(&uid, &username, &department, &created)
checkErr(err)
c = make(map[int]data)
c[i] = data{uid, username, department, created}
fmt.Println(c[i])
}
http.HandleFunc("/",index)
log.Println("Start")
err1 :=http.ListenAndServe(":9090", nil)
if err1!=nil {
log.Fatal("ListenAndServe:", err1)
}
}
func index(w http.ResponseWriter, r *http.Request) {
db, err := sql.Open("mysql", "root:123456789@tcp(127.0.0.1:3306)/123")//用户:密码@主机/数据库
checkErr(err)
defer db.Close()
rows, err := db.Query("SELECT * FROM userinfo")
checkErr(err)
for rows.Next(){
data := data{}
err = rows.Scan(&data.uid, &data.username, &data.departname, &data.created)
checkErr(err)
base = append(base, data)
}
fmt.Fprintf(w,"%s", base)
}
func checkErr(err error) {
if err != nil {
panic(err)
}
}
這是目前的程式碼
在web上輸出的是
[{1 andy a01 2018-07-05} {2 bob b01 2018-07-04} {3 couch c01 2018-07-03} {5 astaxie software developement 2012-12-09} {6 lin y c so tired 2018-07-05} {7 lin y c so tired 2018-07-05} {8 dean go is too hard to learn 2018-07-05} {9 sour2015 welcome to golang world 2018-07-11} {11 atm dont forget to eat lunch 2018-07-11} {12 atm dont forget to eat lunch 2018-07-11} {17 atm zxczqws 2017-01-12}]
無分行
希望是能夠放入一個表單中,看起來比較整潔
#2