go语言学习--pongo2fasthttpfasthttprouterpgx
一、安装
十余年的双阳网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。营销型网站建设的优势是能够根据用户设备显示端的尺寸不同,自动调整双阳建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联从事“双阳网站设计”,“双阳网站推广”以来,每个客户项目都认真落实执行。
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
` user `
(二)数据库测试:
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>
网站题目:go语言学习--pongo2fasthttpfasthttprouterpgx
网站网址:http://scjbc.cn/article/ggdhcd.html