一、安装

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>