您好,登錄后才能下訂單哦!
一、安裝
OS:Windows 10 X64
go:go1.8.3.windows-amd64.msi
二、安裝golang包
pongo2,fasthttp,fasthttprouter,pgx
1.建立項目目錄
f:/go_prog
2.安裝相關包
Microsoft Windows [版本 10.0.14393]
(c) 2016 Microsoft Corporation。保留所有權利。
C:\Windows\system32>f:
F:\>cd go_prog
F:\go_prog>go get -u github.com/flosch/pongo2
F:\go_prog>go get -u github.com/valyala/fasthttp
F:\go_prog>go get -u github.com/buaazp/fasthttprouter
F:\go_prog>go get github.com/jackc/pgx
三、測試程序目錄
(一)web測試
1.目錄結構
F:\go_prog>
|
|---templates
| |
| |--- index.html
|---template_pongo2.go
2.程序文件
1).template_pongo2.go
package main
import (
"fmt"
"log"
"github.com/flosch/pongo2"
"github.com/buaazp/fasthttprouter"
"github.com/valyala/fasthttp"
)
var tpl_base_dir := ""
// Index is the index handler
func Index(ctx *fasthttp.RequestCtx) {
ctx.SetContentType("text/html")
tpl, err := pongo2.FromFile("templates/index.html")
checkErr(err)
// Now you can render the template with the given
// pongo2.Context how often you want to.
out, err := tpl.Execute(pongo2.Context{"user": "fred"})
checkErr(err)
fmt.Fprint(ctx, out)
}
func main() {
router := fasthttprouter.New()
router.GET("/", Index)
log.Fatal(fasthttp.ListenAndServe(":8080", router.Handler))
}
func checkErr(err error) {
if err != nil {
panic(err)
}
}
2).index.html
<html>
<head>
<title>test pongo2</title>
</head>
<body>
` user `
</body>
</html>
(二)數據庫測試:
1.數據庫初始化
db: godb
table: userinfo
F:\go_prog>psql -U postgres godb
psql (9.6.3)
輸入 "help" 來獲取幫助信息.
godb=#
godb=# \d
關聯列表
架構模式 | 名稱 | 類型 | 擁有者
----------+------------------+--------+----------
public | userinfo | 數據表 | postgres
public | userinfo_uid_seq | 序列數 | postgres
(2 行記錄)
godb=# \d userinfo
數據表 "public.userinfo"
欄位 | 類型 | 修飾詞
------------+------------------------+-------------------------------------------------
uid | integer | 非空 默認 nextval('userinfo_uid_seq'::regclass)
username | character varying(100) | 非空
departname | character varying(500) | 非空
created | date |
索引:
"userinfo_pkey" PRIMARY KEY, btree (uid)
godb=# select * from userinfo;
uid | username | departname | created
-----+----------+------------+------------
2 | Peter | cto | 2017-08-17
(1 行記錄)
godb=#
2.db_pgx.go
package main
import (
"fmt"
"github.com/jackc/pgx"
"time"
)
var pool *pgx.ConnPool
type USER struct{
uid int
username string
department string
created time.Time
}
func main() {
var err error
connPoolConfig := pgx.ConnPoolConfig{
ConnConfig: pgx.ConnConfig{
Host:"localhost",
User:"postgres",
Password:"123456",
Database:"godb",
Port: 5432,
},
MaxConnections: 5,
}
pool, err = pgx.NewConnPool(connPoolConfig)
checkErr(err)
rows, err := pool.Query("select * from userinfo")
checkErr(err)
for rows.Next() {
var user USER
err = rows.Scan(&user.uid, &user.username, &user.department, &user.created)
checkErr(err)
fmt.Println( user.uid, user.username, user.department, user.created)
}
}
func checkErr(err error) {
if err != nil {
panic(err)
}
}
3.測試程序運行
F:\go_prog>go run db_pgx.go
2 Peter cto 2017-08-17 00:00:00 +0000 UTC
F:\go_prog>
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。