Hello everyone, I am a wolf at the end of the desert.

站长用Go写的这版博客前台代码比较原生,既没用什么框架,本来想着后面重构后再详细写写分享的,今天群友提了,就简单写写吧。
在线访问地址:https://go.dotnet9.com
Home screenshot:

This article is very simple, but I would like to use a directory:
- Go Basic Learning
- Go Build a blog reference
- Go version blog source code
- Plan later
1. Go Basic Learning
- Go中文文档:https://go.p2hp.com/doc/
- Go web programming: Q group (771992300) has PDF files to share
The above URL and pdf are suitable for laying the foundation and consolidating the basic syntax of Go. The PDF file should not be the book "Go Web Programming"([Singapore] Zheng Zhaoxiong) sold by Jingdong, but it is also very good. It explains a lot of basic web knowledge. It should be a summary by a big shot on the Internet. It is worth reading. The following is the pdf catalog:

The above basic information is recommended for students who have not been exposed to Go. The basics are very important.
2. Go Build a blog reference
The source of the newly reconstructed blog front-end template and Go version of the learning materials:
B站Up主【码神之路】B站首页:https://space.bilibili.com/473844125
Tuition title: [The Road to the God of Codes] Native Go language blog practical tutorial, hands-level project practical tutorial, does not use any framework, easy to understand, explained by a ten-year manufacturer programmer
The video catalog is as follows:

Screenshot of the blog post supporting the video:

The video is suitable for learning and hands-on practice, and the blog post supporting the Up main is suitable for assisting in checking whether the code is correct. The front-end template supporting the blog is shared in the Up main group (group number [718840650]), and the webmaster code warehouse is also uploaded. However, some of them are different from the Up main changes, and some personalized changes are as follows:
- The webmaster did not add front-end article editing: The Dotnet 9 website has a back-end article management function, and the functions overlap.
- The ORM choice is different: the ORM chosen by the webmaster.
- The back-end interface of the Dotnet9 website directly called by the article search webmaster is not implemented in Go: the web API has clear responsibilities with the front desk, and is also shared by other client interfaces. For example, the front desk of the Razor Pages blog also uses the same article search interface.
- others.
3. Go version blog source code
如B站Up主【码神之路】视频教程标题所说“原生Go语言博客实战教程,练手级项目实战教程,未使用任何框架,通俗易懂”,重点是原生,站长实践后发现Up主的路由相关写法与 ASP.NET Core 的Minimal APIs(最小API)相像,当然前者主要是写Web(MVC),后者是写Web API,实践中与自己熟悉的技术比较学习能加深理解,下面对Go版博客源码进行部分简单介绍。
3.1 inlet
main.go为程序入口文件,相当于 ASP.NET Core 的Program.cs文件,文件名可以修改,只要代码第一行为package main。
All codes are as follows:
package main
import (
"log"
"net/http"
"dotnet9.com/goweb/common"
"dotnet9.com/goweb/router"
)
func init() {
// 模板加载:html文件,比如首页文章列表、分类文章列表、页头、页尾等
common.LoadTemplate()
}
func main() {
server := http.Server{
Addr: "127.0.0.1:8080",
}
router.Router()
if err := server.ListenAndServe(); err != nil {
log.Fatal(err)
}
}
服务绑定IP与端口是写死的,可写到配置文件,你就说和如下 ASP.NET Core 最小API像不像吧:
var app = WebApplication.Create(args);
app.MapGet("/", () => "Hello World!");
app.Run("http://localhost:3000");
router.Router()是简单封装的路由(封装如下),和上面最小APIapp.MapGet("/", () => "Hello World!");像几分,能给到8分不?:
Simple routing encapsulation of go: ** router. go **
package router
import (
"net/http"
"dotnet9.com/goweb/views"
"dotnet9.com/goweb/api"
)
func Router() {
http.HandleFunc("/", views.HTML.Index)
http.HandleFunc("/c/", views.HTML.Category)
http.HandleFunc("/p/", views.HTML.Detail)
http.HandleFunc("/login", views.HTML.Login)
http.HandleFunc("/api/v1/post", api.API.SaveAndUpdatePost)
http.Handle("/resource/", http.StripPrefix("/resource/", http.FileServer(http.Dir("public/resource/"))))
}
The routing home page code is as follows:
./views/index.go
package views
import (
"errors"
"log"
"net/http"
"strconv"
"dotnet9.com/goweb/common"
"dotnet9.com/goweb/service"
)
func (*HTMLApi) Index(w http.ResponseWriter, r *http.Request) {
// 这里是业务代码,读取文章列表,绑定模板
}
3.n Template Binding
Skip the use of orm and talk about template binding:
This is Go's article list template: ./ template/layout/post-list.html
{{define "post-list"}}
<ul class="post-box">
{{range $index, $elem := .Posts}}
<li class="post-label">
<a href="/p/{{$elem.Slug}}"><h2>{{$elem.Title}}</h2></a>
<p>{{$elem.Description}}</p>
<div class="post-action">
<span>
<i class="iconfont icon-yonghu"></i>
{{$elem.Original}}
</span>
<span>
<i class="iconfont icon-wenjianjia"></i>
{{range $catIndex, $cat := $elem.Categories}}
<a href="/c/{{$cat.Slug}}">{{$cat.Name}}</a>
{{end}}
</span>
<span>
<i class="iconfont icon-riqi"></i>
{{$elem.CreationTime}}
</span>
<span>
<i class="iconfont icon-yanjing"></i>
{{$elem.ViewCount}}
</span>
</div>
</li>
{{end}}
</ul>
{{end}}
对比这是Razor Pages文章列表模板绑定:
@page
@inject IOptionsSnapshot<SiteOptions> SiteOptions
@using Dotnet9.Web.ViewModel.Commons
@model IndexModel
@{
ViewData["Title"] = "首页";
}
<ul class="post-box">
@if (Model.BlogPosts?.Any() == false)
{
<span>没有数据哦</span>
}
else
{
@foreach (var blogPost in Model.BlogPosts!)
{
<li class="post-label">
<a href="@blogPost.CreationTime.ToString("/yyyy/dd")/@blogPost.Slug">
<h2>@blogPost.Title</h2>
</a>
<p>@blogPost.Description</p>
<div class="post-action">
<span>
<i class="iconfont icon-yonghu"></i>
@(blogPost.Original.IsNullOrWhiteSpace() ? SiteOptions.Value.Owner : blogPost.Original)
</span>
<span>
<i class="iconfont icon-wenjianjia"></i>
@foreach (var cat in blogPost.Categories)
{
<a href="/cat/@cat.Slug" title="@cat.Description">@cat.Name</a>
}
</span>
<span>
<i class="iconfont icon-riqi"></i>
@blogPost.CreationTime.ToString("yyyy-MM-dd")
</span>
<span>
<i class="iconfont icon-yanjing"></i>
@blogPost.ViewCount
</span>
</div>
</li>
}
}
</ul>
@await Html.PartialAsync("_PaginationPartial", new PaginationModel(Model.Current, Model.Pages, Model.Total, Model.PageSize, Model.PageCount))
Take a rough look at the code:
- Go模板绑定使用
{{.对象字段}},注意前面的点 - Razor语法绑定使用
@Model.对象
Languages are all similar. If you are familiar with one technology stack, you can follow other technology stacks by analogy.
3.n +1 comments
评论对接的第三接口Valine,后面考虑自己开发:
Valine-A fast, concise and efficient non-backend comment system.
-
- Characteristics **
- rapid
- security
- Emoji 😉
- No backend implementation
- MarkDown full syntax support
- Lightweight and easy to use
- Article reading statistics v1.2.0 +
Screenshots of website usage:

Valine message background management:

4. Plan later
Learning Go, or a new technology, you can't just learn it. You need to find opportunities to use it. Try using Go to do a project that you are interested in, such as developing a simple blog front desk. I believe that you will learn more motivated. At first, you can try native development, that is, without using a framework, which is very helpful for understanding the basic syntax of a language.
当想快速进行业务开发、迭代时,可以考虑框架了,站长最近有关注 goframe:
GoFrame is a modular, high-performance, enterprise-level Go basic development framework. GoFrame is a universal basic development framework and an enhanced extension of the Golang standard library. It contains basic development components with common core. Its advantages are practical, modular, comprehensive documentation, rich modules, high ease of use, and versatility. Strong, team-oriented. If you want to use Golang to develop a business-oriented project, whether it is a small, medium to large project, GoFrame is your choice. If you want to develop a Golang component library, GoFrame provides out-of-the-box, rich and powerful basic component library that can help you do your work with half the effort. If you are a team leader, GoFrame's rich documentation, detailed code comments, and active community members will greatly reduce your guidance costs and support rapid team access, language transformation and ability improvement.
GoFrame official document: https://www.example.com pageId=1114119

后面就用goframe重构这版go博客吧,当然前提是先把Razor Pages版本博客开发完善了,包括后台...
The hydrology ended (it took 2 hours), as long as you are willing to spend the time learning, there is nothing you can't learn. If you refuse to accept it, you can only say one word: correct.