当前位置: 首页 > news >正文

公司备案证查询网站查询wordpress获取某个分类下所有分类

公司备案证查询网站查询,wordpress获取某个分类下所有分类,哈尔滨建设鞋城,则么建立自己的网站文章目录 一、项目简介二、项目搭建前置知识三、首页- - -前端文件与后端结构体定义四、配置文件加载五、构造假数据- - -显示首页内容 代码地址#xff1a;https://gitee.com/lymgoforIT/goblog 一、项目简介 使用Go原生http开发一个简易的博客系统#xff0c;包含一下功能… 文章目录 一、项目简介二、项目搭建前置知识三、首页- - -前端文件与后端结构体定义四、配置文件加载五、构造假数据- - -显示首页内容 代码地址https://gitee.com/lymgoforIT/goblog 一、项目简介 使用Go原生http开发一个简易的博客系统包含一下功能 文章增删改查文章列表分页显示评论系统文章分类、归档 学习本项目能学到什么 使用Go开发web项目基本思路初步具有工程思维如路由分组、目录组织代码封装等。博客基本功能开发套路如博客与评论的查询、分类、归档、分页等。循序渐进掌握编程思维和思路。这一点是最重要的会不断优化代码而不是一步给出最终代码这样更能培养编程思维和思路。 页面效果 二、项目搭建前置知识 接下来我们会一步一步实现博客首页开始我们可能会把代码都写到main.go中后期再不断调整优化形成工程化的目录结构。 首先是启动一个http服务的代码如下 package mainimport (lognet/http )func main(){server : http.Server{Addr: 127.0.0.1:8080,}http.HandleFunc(/, func(writer http.ResponseWriter, request *http.Request) {writer.Write([]byte(ok))})if err : server.ListenAndServe();err ! nil {log.Println(err)} } 启动后浏览器访问可以看到ok字样 这里返回给前端的不过是一个字符串而已实际工作中一般前后端交互都是使用的JSON数据或者protoc协议的那么想要返回JSON字符串给前端又该如何做呢 其实也简单设置好请求头即可如下 package mainimport (encoding/jsonlognet/http )type IndexData struct {Title string json:titleDesc string json:desc }func index(w http.ResponseWriter, r *http.Request) {// 设置请求头指明返回的是JSON数据w.Header().Set(Content-Type, application/json)var indexData IndexDataindexData.Title go博客indexData.Desc 入门学习笔记jsonStr, _ : json.Marshal(indexData)w.Write(jsonStr) }func main() {server : http.Server{Addr: 127.0.0.1:8080,}http.HandleFunc(/, index)if err : server.ListenAndServe(); err ! nil {log.Println(err)} } 目前浏览器得到的响应结果都是后端直接返回的数据但我们前端是给用户使用的需要有一定页面才行。 在Go中渲染html页面可以使用Go自带的html/template库使用方式如下 首先建立template/index.html文件 !DOCTYPE html html langen headmeta charsetUTF-8titleTitle/title /head body hello lym blog!! br 标题{{.Title}} br 描述{{.Desc}} /body /htmlpackage mainimport (encoding/jsonfmthtml/templatelognet/httpos )type IndexData struct {Title string json:titleDesc string json:desc }func index(w http.ResponseWriter, r *http.Request) {// 设置请求头指明返回的是JSON数据w.Header().Set(Content-Type, application/json)var indexData IndexDataindexData.Title go博客indexData.Desc 入门学习笔记jsonStr, _ : json.Marshal(indexData)w.Write(jsonStr) }func indexHtml(w http.ResponseWriter, r *http.Request) {// 使用给定的名字分配一个html模板t : template.New(index.html)viewPath, _ : os.Getwd()// 将html文件关联到模板上t, _ t.ParseFiles(viewPath /template/index.html)var indexData IndexDataindexData.Title go博客indexData.Desc 入门学习笔记// 使用给定的数据结构解析模板并将结果写入werr : t.Execute(w, indexData)fmt.Println(err) }func main() {server : http.Server{Addr: 127.0.0.1:8080,}http.HandleFunc(/, index)http.HandleFunc(/index, indexHtml)if err : server.ListenAndServe(); err ! nil {log.Println(err)} } 此时访问index路径便会看到是页面展示了不过还没有css,js等静态文件渲染后期会慢慢加上。 三、首页- - -前端文件与后端结构体定义 有了前面的基础知识我们现在就可以搭建首页啦 因为本学习笔记主要注重的是后端逻辑所以前端页面和静态文件等是直接用的已有的放到项目目录下即可。如果有需要可以到码云上取https://gitee.com/lymgoforIT/goblog 首页展示时有很多数据是从后端获取的所以我们首先需要定义一些结构体用于数据渲染其中有一些通用数据且基本不怎么变的我们可以放到配置文件中而不用存DB比如博客系统的标题、标签以及相关系统变量等。 config/config.go package configtype Viewer struct {Title string // 首页标题Description string // 首页描述Logo string // 博客系统logoNavigation []string // 导航栏Bilibili string // bilibiliAvatar string // 头像UserName string // 用户名UserDesc string // 用户描述 }// 系统相关配置 type SystemConfig struct {AppName stringVersion float32CurrentDir string // 项目路径CdnURL string // cdn地址用户缓存静态资源QiniuAccessKey string // 使用七牛云存储图片资源QiniuSecretKey stringValine bool // 评论系统用的ValineValineAppid stringValineAppkey stringValineServerURL string } 其余数据基本是从DB获取的与DB交互的结构体我们一般会单独建立model文件夹存放。 models/post.go package modelsimport (goblog/confightml/templatetime )// 用于与DB交互与DB中字段一一对应 type Post struct {Pid int json:pid // 文章IDTitle string json:title // 文章标题Slug string json:slug // 自定义页面 pathContent string json:content // 文章的htmlMarkdown string json:markdown // 文章的MarkdownCategoryId int json:categoryId //分类idUserId int json:userId //用户idViewCount int json:viewCount //查看次数Type int json:type //文章类型 0 普通1 自定义文章CreateAt time.Time json:createAt // 创建时间UpdateAt time.Time json:updateAt // 更新时间 }// 用于给前端响应所以有了分类名称和用户名等信息而不是分类id或者用户id type PostMore struct {Pid int json:pid // 文章IDTitle string json:title // 文章标题Slug string json:slug // 自定义页面 pathContent template.HTML json:content // 文章的htmlCategoryId int json:categoryId // 文章的MarkdownCategoryName string json:categoryName // 分类名UserId int json:userId // 用户idUserName string json:userName // 用户名ViewCount int json:viewCount // 查看次数Type int json:type // 文章类型 0 普通1 自定义文章CreateAt string json:createAtUpdateAt string json:updateAt }type PostReq struct {Pid int json:pidTitle string json:titleSlug string json:slugContent string json:contentMarkdown string json:markdownCategoryId int json:categoryIdUserId int json:userIdType int json:type }type SearchResp struct {Pid int orm:pid json:pid // 文章IDTitle string orm:title json:title }type PostRes struct {config.Viewerconfig.SystemConfigArticle PostMore } models/home.go 用于封装前端需要的首页数据 package modelsimport goblog/configtype HomeResponse struct {config.ViewerCategorys []CategoryPosts []PostMoreTotal intPage intPages []intPageEnd bool // 当前页是否是最后一页决定分页那里是否显示左右箭头 } 四、配置文件加载 这里配置文件我们用的toml实际工作中可能yaml用的更多一点。 config/config.toml [viewer]Title Go语言博客Description Go语言博客Logo /resource/images/logo.pngNavigation [首页,/, GO语言,/golang, 归档,/pigeonhole, 关于,/about]Bilibili https://space.bilibili.com/473844125Zhihu https://www.zhihu.com/people/ma-shen-zhi-luAvatar https://gimg2.baidu.com/image_search/srchttp%3A%2F%2Finews.gtimg.com%2Fnewsapp_bt%2F0%2F13147603927%2F1000.jpgreferhttp%3A%2F%2Finews.gtimg.comapp2002sizef9999,10000qa80n0g0nfmtjpeg?sec1647242040tc6108010ed46b4acebe18955acdd2d24UserName 张三UserDesc 长得非常帅的程序员 [system]CdnURL https://static.mszlu.com/goblog/es6/md-assetsQiniuAccessKey 替换自己的QiniuSecretKey 替换自己的Valine trueValineAppid 替换自己的ValineAppkey 替换自己的ValineServerURL 替换自己的toml文件我们可以使用github.com/BurntSushi/toml包获取 go get github.com/BurntSushi/toml配置文件加载常规套路是定义全局变量使用init加载 config/config.go package configimport (osgithub.com/BurntSushi/toml )var Cfg *TomlConfigfunc init() {Cfg new(TomlConfig)var err errorCfg.System.CurrentDir, err os.Getwd()if err ! nil {panic(any(err))}Cfg.System.AppName lym-go-blogCfg.System.Version 1.0_, err toml.DecodeFile(config/config.toml, Cfg)if err ! nil {panic(any(err))} }type TomlConfig struct {Viewer ViewerSystem SystemConfig }type Viewer struct {Title string // 首页标题Description string // 首页描述Logo string // 博客系统logoNavigation []string // 导航栏Bilibili string // bilibiliAvatar string // 头像UserName string // 用户名UserDesc string // 用户描述 }// 系统相关配置 type SystemConfig struct {AppName stringVersion float32CurrentDir string // 项目路径CdnURL string // cdn地址用户缓存静态资源QiniuAccessKey string // 使用七牛云存储图片资源QiniuSecretKey stringValine bool // 评论系统用的ValineValineAppid stringValineAppkey stringValineServerURL string } 五、构造假数据- - -显示首页内容 main.go 注意看代码注释 package mainimport (goblog/configgoblog/modelshtml/templatelognet/httptime )type IndexData struct {Title string json:titleDesc string json:desc }// 前端html页面中使用了一些函数所以这里需要定义一下 // 是否偶数 func IsODD(num int) bool {return num%2 0 }func GetNextName(strs []string, index int) string {return strs[index1] }// 日期按指定格式转换 func Date(layout string) string {return time.Now().Format(layout) }func index(w http.ResponseWriter, r *http.Request) {t : template.New(index.html)// 拿到当前的路径path : config.Cfg.System.CurrentDir//访问博客首页模板的时候因为有多个模板的嵌套解析文件的时候需要将其涉及到的所有模板都进行解析home : path /template/home.htmlheader : path /template/layout/header.htmlfooter : path /template/layout/footer.htmlpersonal : path /template/layout/personal.htmlpost : path /template/layout/post-list.htmlpagination : path /template/layout/pagination.html // 页码// 定义模板中需要用到的函数t.Funcs(template.FuncMap{isODD: IsODD, getNextName: GetNextName, date: Date})t, err : t.ParseFiles(path/template/index.html, home, header, footer, personal, post, pagination)if err ! nil {log.Println(err)}//页面上涉及到的所有的数据必须有定义var categorys []models.Category{{Cid: 1,Name: go,},{Cid: 2,Name: python,},}var posts []models.PostMore{{Pid: 1,Title: go博客,Content: 这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容,UserName: 张三,ViewCount: 123,CreateAt: 2023-12-17,CategoryId: 1,CategoryName: go,Type: 0,},{Pid: 2,Title: 这是第二篇博客,Content: 这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容这里是内容,UserName: 李四,ViewCount: 1314,CreateAt: 2023-12-17,CategoryId: 1,CategoryName: go,Type: 0,},}var hr models.HomeResponse{Viewer: config.Cfg.Viewer,Categorys: categorys,Posts: posts,Total: 2,Page: 1,Pages: []int{1},PageEnd: true,}t.Execute(w, hr) }func main() {//程序入口一个项目 只能有一个入口//web程序http协议 ip portserver : http.Server{Addr: 127.0.0.1:8080,}http.HandleFunc(/, index)// 因为静态文件放到了public/resource目录下但是页面中写路径的时候都写的resource所以这里转一下http.Handle(/resource/, http.StripPrefix(/resource/, http.FileServer(http.Dir(public/resource/))))if err : server.ListenAndServe(); err ! nil {log.Println(err)} } 此时通过流量器访问8080端口便可看到如下界面了
http://www.pierceye.com/news/534754/

相关文章:

  • ic外贸网站建设wordpress和shopex
  • 网站技术制作流程图国内顶尖小程序开发公司
  • 免费网站建设下载优化关键词规则
  • 网站浮动窗口如何做自己怎么做淘宝客网站
  • 石材外贸在哪个网站做网页版 微信
  • 网站开发属于程序员吗sem 优化软件
  • 公司做网站是管理费用小程序官方文档
  • 公司网站推广技巧响水网站设计
  • 徐州本地网站wap页面是什么
  • 网站开发应用价值做套网站多少钱
  • asp.net网站模板免费下载怎么才能访问自己做的网站
  • 长沙企业网站制作宝安公司网站建设
  • 做网站需要拉多大的宽带dw做的网站怎么做后台
  • 公司网站建设设计公司哪家好wordpress自动封ip
  • 郫县网站制作wordpress搜索打钩
  • 哪些网站可以做招商广告语wordpress发文章的id怎么不连续
  • 家私网站栏目和功能需求策划网页样式库
  • 什么是网站网页主页企业电子邮箱格式
  • 金属建材企业网站建设方案用pycharm做网站
  • 重庆网站空间黄骅港一期码头潮汐表
  • 推广网站如何做做酒店网站所用到的算法
  • 最好的网站建设组织wordpress 删除google
  • 生物科技 网站模板下载在线室内设计
  • 网站兼容性问题线上设计师接单
  • 外包网站平台可以做电算化的网站
  • 教育网站设计案例学校网站设计
  • 网站建设入门教程pdf网络推广和seo
  • 闲鱼钓鱼网站怎么做百度网页版主页
  • 一次备案多个网站alexa排名查询
  • 郑州做招商的网站网站建设的流程推广方案