为了账号安全,请及时绑定邮箱和手机立即绑定

go语言学习--pongo2

标签:
Go

一、安装

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>


点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消