go_web_programming_sg

module
v0.0.0-...-4751b8d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 24, 2019 License: MIT

README

Go Web编程 [新加坡] 郑兆雄(Sau,Sheong,Chang)

郑老师的 <Go Web编程> 是一本很好的web编程入门书籍, 该书讲解比较贴近生产环境,不像其他资料,只给出一个功能简单没什么实际用途的的helloworld 页面网站.

唯一的缺点就是代码实例不是很详细,比如第二章, 直接跳到章节末尾运行程序是,可以执行的, 但中间步骤没有git提交, 对照文章学习很不方便, 在这里,**我会记录下自己阅读每一节的提交代码, **,也方便给同好一个印证.

说明:

  1. 对应go web 书籍请在京东自行购买,
  2. 随书源码下载 https://github.com/sausheong/gwp,这里是阅读笔记

第一章

  • 06-05 18:25 init 环境 程序运行需要相应的包环境,比如我的: GOPATH=C:\Users\yann\go 项目目录: C:\Users\yann\go\src\yann\Go_Web_Programming_sg\chitchat

如果不确认可也以用原文的地址 C:\Users\yann\go\src\github.com\sausheong\gwp

  • 03-23 16:05 1.10 first_webapp
    项目很简单, 进入目录 运行即可, 最后网页上可以看到 Hello World!

第二章

  • 04-15 16:14 2.4.1 chitchat
    开始有代码, 尝试运行一下会报找不到 index. 正常报错 因为index函数还没写
    .\main.go:14:22: undefined: index

  • 04-15 16:35 2.4.3 chitchat
    涉及的template,和data包现在还没有写,依然无法运行 .\main.go:28:15: undefined: template .\main.go:29:18: undefined: data

  • 04-15 17:46 2.4.4 chitchat
    新增2个文件,修改1个,报错更多,因为涉及的模块还没写,继续下去 .\main.go:35:22: cannot refer to unexported name data.threads
    .\main.go:35:22: undefined: data.threads
    .\main.go:36:20: undefined: session
    .\main.go:45:14: undefined: templates
    .\main.go:45:34: template.Must undefined (type *"html/template".Template has no field or method Must)
    .\main.go:47:14: undefined: templates
    .\main.go:47:34: template.Must undefined (type *"html/template".Template has no field or method Must)
    .\main.go:49:10: undefined: templates

  • 04-15 18:23 2.5.0 chitchat
    新增3个模版文件,修改2个文件,仍然在报错,书上的代码和git上提供的,也有些不一样了
    .\main.go:31:25: cannot refer to unexported name data.threads
    .\main.go:31:25: undefined: data.threads
    .\main.go:32:23: undefined: session
    .\main.go:34:17: undefined: generateHTML
    .\main.go:36:17: undefined: generateHTML

  • 04-15 18:48 2.7.0 chitchat 好消息是pg数据库可以用了,命令行访问成功,对照源码做了一些修改, 仍然无法启动,不过报错在减少
    .\main.go:34:4: undefined: error_message
    .\main.go:36:23: undefined: session
    .\main.go:38:17: undefined: generateHTML
    .\main.go:40:17: undefined: generateHTML

  • 04-15 19:00 2.8.0 chitchat 第二章的最后一节,我们自己敲的代码仍然没有运行起来,毕竟让其运行起来是全书的内容
    不过对一下午的劳动有个小奖励: 使用下载的源码,连上数据库是可以运行起来的,使用 go build
    注意项目路径 C:\Users\yann\go\src\github.com\sausheong\gwp\Chapter_2_Go_ChitChat\chitchat
    访问 http://localhost:8080

第三章

  • 04-16 10:33 3.2.1 Handling_Requests 第三章的第一次代码,运行成功,最简单的Web服务器,默认端口80访问
    404 page not found

  • 04-16 11:01 3.2.2 Handling_Requests 配置带证书的web服务器,及自己生成证书,运行成功
    go run gencert.go go run server.go 注意,这次要用 https 访问, 运行成功,出现404页面 404 page not found

  • 04-16 11:53 3.3.1 Handling_Requests 修改了默认的多路复用器,访问任何地址都显示hello world
    访问 localhost:8080
    访问 localhost:8080/abc

  • 04-16 13:29 3.3.2 Handling_Requests 使用多个处理器,处理请求
    访问 localhost:8080/hello
    访问 localhost:8080/world

  • 04-16 13:38 3.3.3 Handling_Requests 使用处理器函数,处理请求
    访问 localhost:8080/hello
    访问 localhost:8080/world

  • 04-16 14:30 3.3.4 Handling_Requests 串联多个处理器或处理器函数
    访问 localhost:8080/hello 注意看控制台输出

  • 04-16 15:23 3.3.6 Handling_Requests 换用第三方多路复用器
    访问 localhost:8080/hello/yann

  • 04-16 15:42 3.4.0 Handling_Requests 导入包,启用http2,花费了很多时间,最终测试成功
    curl.exe -I --http2 --insecure https://localhost:8080/
    HTTP/2 200
    content-type: text/plain; charset=utf-8
    content-length: 17
    date: Tue, 16 Apr 2019 08:48:15 GMT

第四章

  • 04-17 15:18 4.1.3 Processing_Requests 处理请求,首部 header
    访问 localhost:8080/headers 显示出首部信息

  • 04-17 15:47 4.1.4 Processing_Requests 处理请求,主体 body
    curl -id "first_name=yann&last_name=cao" 127.0.0.1:8080/body

  • 04-17 16:18 4.2.1 Processing_Requests 处理请求,表单 Form
    双击 client.html 文件,打开返回如下:
    map[hello:[yann world] post:[456] thread:[123]]

  • 04-17 17:28 4.2.2 Processing_Requests 处理请求,表单方法 PostForm
    双击 client.html 文件,打开返回如下:
    map[hello:[yann] post:[456]]

  • 04-17 18:17 4.2.3 Processing_Requests 处理请求,表单方法 ParseMultipartForm
    双击 client.html 文件,打开返回如下:
    &{map[hello:[sau sheong] post:[456]] map[]}

  • 04-18 14:21 4.2.4 Processing_Requests 测试文件上传 fileupload
    双击 client.html 文件,选择hello文本文件上传,会返回文件内容:

  • 04-18 14:44 4.3.0 Processing_Requests 处理请求,返回 json数据, 访问对应域名返回 json格式数据(1行)
    curl -i 127.0.0.1:8080/json

  • 04-18 16:06 4.4.2 Processing_Requests 设置cookie
    注意cookie不能直接看到,需要点浏览器地址栏左边的小锁,或者使用postman
    second_cookie "yann's test" 127.0.0.1

  • 04-18 16:59 4.4.3 Processing_Requests 处理请求,获取 cookie, 并返回
    curl -i 127.0.0.1:8080/get_cookie
    返回数据如下
    [first_cookie="Go Web Programming"; second_cookie="yann's test"]

  • 04-18 17:59 4.4.4 Processing_Requests 处理请求, flash 短信, 做一个看完就消失的短信,需要访问三次
    curl -i 127.0.0.1:8080/set_message // 检查 cookie,确认信息 curl -i 127.0.0.1:8080/show_message 返回 Hello World! curl -i 127.0.0.1:8080/show_message 返回 No message found

第五章

  • 04-23 18:55 5.21 base template 初步了解下模板的使用方法
    验证方法 浏览器访问 127.0.0.1:8080/process

  • 04-24 16:32 5.3.1 random_number 动作-条件动作 if
    生成一个随机数,根据数字显示不同的文字 浏览器访问 127.0.0.1:8080/process

  • 04-24 18:35 5.3.2 iterator 动作-迭代动作 range
    迭代显示出一周星期数 浏览器访问 127.0.0.1:8080/process

  • 04-24 18:41 5.3.3 set_dot 动作-设置动作 set
    设置字段 浏览器访问 127.0.0.1:8080/process

  • 04-24 19:05 5.3.4 include 动作-包含动作 include
    设置字段 浏览器访问 127.0.0.1:8080/process

  • 04-25 11:26 5.5.0 custom_function 自定义函数
    日期转换函数, 数字显示有些异常 浏览器访问 127.0.0.1:8080/process

  • 04-25 13:24 5.6.0 context_aware 上下文感知
    如果模板显示的是 html 格式的内容,那么模板将对其实施 html 转义
    如果模板显示的是 JavaScript 格式的内容,那么模板将对其实施 JavaScript 转义
    浏览器访问 127.0.0.1:8080/process

  • 04-25 13:41 5.6.1 XSS 防御XSS攻击
    浏览器访问 127.0.0.1:8080/form
    攻击脚本:

  • 04-25 14:59 5.6.2 no escape 取消转
    这个测试失败了, 无论 chrome还是火狐全没有显示出弹窗 浏览器访问 127.0.0.1:8080/form
    攻击脚本:
  • 04-25 15:26 5.7.0 content 模板嵌套
    多次刷新浏览器,有时显示红色字符,有时蓝色
    浏览器访问 127.0.0.1:8080/process

  • 04-25 16:03 5.8.0 block action 块动作定义默认模板
    多次刷新浏览器,有时显示红色字符,有时蓝色
    浏览器访问 127.0.0.1:8080/process

第六章

  • 05-14 15:55 6.1.0 store memory 把数据以数据结构的方式存储在内存中
    结果:在命令行中打印出存储的数据

  • 05-14 17:19 6.2.0 read_write_files 把数据以数据结构的方式存储在文本文件中
    结果:在命令行中打印出存储的数据
    在程序目录也可以看到生成的txt文件

  • 05-14 17:38 6.2.1 csv_store 把数据以数据结构的方式存储在csv文件中
    结果:在命令行中打印出存储的数据
    在程序目录也可以看到生成的csv文件

  • 05-14 18:39 6.2.2 gob_store 把数据以数据结构的方式存储在gob文件中,这是go独有的一种二进制格式
    结果:在命令行中打印出存储的数据
    在程序目录也可以看到生成的二进制文件,当然看不到内容

  • 05-15 10:21 6.3.1 sql store base 把数据以数据结构的方式存储在数据库中
    这一章实现了数据库的基本CURD操作,内容非常的多
    结果:在命令行中打印出数据库存储的数据

  • 05-15 15:34 6.3.2 sql comment 表之间建立关联,一对多,多对一,帖子和评论的相互关系
    这一章讲述了数据之间的对应关系,这是数据库编程的基础
    结果:在命令行中打印出数据库存储的帖子及相关评论

  • 05-15 16:29 6.5.1 Sqlx 使用第三方ORM软件sqlx 来操作数据库
    结果:在命令行中打印出存储的数据

  • 05-15 18:19 6.5.2 gorm 使用第三方ORM软件gorm 来操作数据库
    结果:在命令行中打印出存储的数据

第七章

  • 05-21 21:20 7.4.1 xml_parsing_decoder 通过 Unmarshal 解析xml文件
    通过 Decoder 解析xml文件
    结果:在命令行中查看解析结果

  • 05-23 16:27 7.4.2 xml_creating_marshal 通过 Unmarshal 构建 xml文件
    通过 Decoder 构建 xml文件
    结果:文件浏览器中查看生成文件内容

  • 05-23 18:36 7.5.1 json_parsing_unmarshal 通过 Unmarshal 解析json文件
    通过 Decoder 解析json文件
    结果:在命令行中查看解析结果

  • 05-23 21:10 7.5.2 json_creating_marshal 通过 Unmarshal 构建 json 文件
    通过 Decoder 构建 json 文件
    结果:文件浏览器中查看生成文件内容

  • 05-24 10:20 7.6.0 web service 前期知识汇总, 测试增删改查操作

curl -i -X POST -H "Content-Type: application/json" -d '{"content":"My first post","author":"Sau Sheong"}' http://127.0.0.1:8080/post/

curl -i -X GET http://127.0.0.1:8080/post/1

curl -i -X PUT -H "Content-Type: application/json" -d '{"content":"Updated post","author":"yann"}' http://127.0.0.1:8080/post/1

curl -i -X DELETE http://127.0.0.1:8080/post/1

结果:完成相关操作后,在数据库中看结果,详情也可以看公众号
扫描下方二维码加入


Go_Web_Programming_sg go web

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL