我有一些代码可以在 Postgres DB 中创建表
import (
"github.com/jinzhu/gorm"
_ "github.com/lib/pq"
)
type Table struct {
Id int `gorm:"primary_key"`
Name string `gorm:"type:varchar(100)"`
Addr string `gorm:"type:varchar(100)"`
}
func main() {
db, _ := gorm.Open("postgres", "user=postgres password=poilo777 dbname=mydb sslmode=disable")
defer db.Close()
db.CreateTable(&Table{})
user := &Table{Name: "ololo", Addr: "pololo"}
Run Code Online (Sandbox Code Playgroud)
我面临两个问题:1)在数据库中创建了一个表“tables”而不是“Table”2)如何在现有的另一个表中插入数据?(例如“用户”)
我想在 go 包“模板”中用 HTML 制作表格,我想在循环中添加行,但我没有找到如何做到这一点
我的代码:
package main
import (
"net/http"
"html/template"
)
type Devicevalue_view struct {
Devicetype string
Iddevice string
Devicename string
Oidname string
Value string
}
func page_1(w http.ResponseWriter, r *http.Request){
for i:=1; i<10; i++{
data := Devicevalue_view{
Devicetype: "devicetype",
Iddevice: "iddevice",
Devicename: "devicename",
Oidname: "oidname",
Value: "value",
}
tmpl, _ := template.ParseFiles("./index.html")
tmpl.Execute(w, data)
}
}
func main() {
http.HandleFunc("/table", page_1)
http.ListenAndServe(":3000", nil)
}
Run Code Online (Sandbox Code Playgroud)
我得到这个:
Devices
Type Name Param Time Value
devicetype iddevice devicename oidname value
Devices …Run Code Online (Sandbox Code Playgroud)