go语言学习--pongo2 fasthttp fasthttprouter pgx

简介:

一、安装

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>


本文转自 pgmia 51CTO博客,原文链接:http://blog.51cto.com/heyiyi/1957404


相关文章
|
10天前
|
Go
go语言中的数据类型
go语言中的数据类型
11 0
|
16天前
|
Go 开发者
掌握Go语言:Go语言结构体,精准封装数据,高效管理实体对象(22)
掌握Go语言:Go语言结构体,精准封装数据,高效管理实体对象(22)
|
16天前
|
安全 Go
掌握Go语言:Go语言通道,并发编程的利器与应用实例(20)
掌握Go语言:Go语言通道,并发编程的利器与应用实例(20)
|
16天前
|
存储 缓存 安全
掌握Go语言:Go语言中的字典魔法,高效数据检索与应用实例解析(18)
掌握Go语言:Go语言中的字典魔法,高效数据检索与应用实例解析(18)
|
16天前
|
Go
使用Go语言发邮件
使用Go语言发邮件
20 2
|
28天前
|
缓存 安全 Java
Go语言小细节
Go语言小细节
36 0
|
1月前
|
存储 安全 Go
|
16天前
|
存储 安全 Go
掌握Go语言:Go语言类型转换,无缝处理数据类型、接口和自定义类型的转换细节解析(29)
掌握Go语言:Go语言类型转换,无缝处理数据类型、接口和自定义类型的转换细节解析(29)
|
1天前
|
前端开发 Java Go
开发语言详解(python、java、Go(Golong)。。。。)
开发语言详解(python、java、Go(Golong)。。。。)
|
10天前
|
存储 Java 编译器
go语言基础语法
go语言基础语法