当前位置:网站首页>Use go language to complete the student information management system through restful API
Use go language to complete the student information management system through restful API
2022-04-21 14:24:00 【Chen XiaoC】
Use Go Language passing RestfulAPI Complete the student information management system
List of articles
One 、 Write at the beginning
This article mainly introduces how to use golang Medium beego frame , follow MVC framework , And USES the restful Style api To reconstruct the student management system I wrote before , In all, we have achieved 5 Five interfaces correspond to five functions
Use postman perhaps apifox adopt json Format data and back-end interaction , Why not make a page , Because I don't know , Later, I will learn to make an interface .
About Python Version has been implemented in my previous article , Yes Python Interested friends can read the following two blogs :
- Python Realize a simple student information management program
- Python Implement a simple student information management program and save the data to Mysql database ( Source code attached )
Two 、API file
API For request examples and returns, please refer to : On-line API file

3、 ... and 、 The code structure

- Use go mod Management dependence , Executable go mod tidy Fast download dependency
- common Modules store public services , initialization mysql, Import the configuration in the configuration file
- controller The module is mainly responsible for logical judgment , And call the corresponding database function
- database The module is responsible for interacting with the database , And return the corresponding data to controller
- modules Modules store student models
- router Module responsible beego Route forwarding of
- server The module is responsible for starting the project
Four 、 Function display
4.1 New student information

4.2 Delete the specified student information

4.3 Query individual student information

4.4 Query all student information

4.5 Get the number of students

5、 ... and 、 Part of the code shows
The two most critical functions controller And database
5.1 controllers
package controllers
import (
"encoding/json"
"errors"
"log"
"strconv"
"student/database"
"student/modules"
"github.com/astaxie/beego"
)
type Student struct {
beego.Controller
}
var Stu = new(Student)
// Add student information
func (s *Student) Add() {
// json Deserialization
err := json.Unmarshal(s.Ctx.Input.RequestBody, modules.StuRes)
if err != nil {
log.Println("json Unmarshal err", err)
s.Ctx.WriteString(errors.New("json Unmarshal err").Error())
return
}
// Judge whether students exist
ok := database.IsExits(modules.StuRes.ID)
if ok {
s.Ctx.WriteString("Student already exists")
return
}
// Add student information
err = database.InsertStu(modules.StuRes)
if err != nil {
log.Println("insert student info err", err)
s.Ctx.WriteString(errors.New("insert student info failed").Error())
return
}
s.Ctx.WriteString("save ok")
log.Println("add student info success")
}
// View all student information
func (s *Student) Show() {
// Decide whether to pass in id perhaps name
id, _ := strconv.Atoi(s.Ctx.Input.Query("id"))
name := s.Ctx.Input.Query("name")
if id != 0 || name != "" {
s.QueryByIDOrName(id, name)
return
}
sturess, err := database.ShowStu(modules.StuRes)
if err != nil {
s.Ctx.WriteString(err.Error())
return
}
s.Data["json"] = sturess
s.ServeJSON()
log.Println("show student info success")
}
func (s *Student) Delete() {
tmp := s.Ctx.Input.Query("id")
id, _ := strconv.Atoi(tmp)
ok := database.IsExits(id)
if !ok {
s.Ctx.WriteString("id not exits")
return
}
err := database.DeleteStu(id)
if err != nil {
s.Ctx.WriteString(err.Error())
return
}
s.Ctx.WriteString("delete ok")
log.Println("delete student info success")
}
func (s *Student) QueryByIDOrName(id int, name string) {
ok := database.IsExits(id)
if !ok {
s.Ctx.WriteString("student not exits")
return
}
sturess, err := database.QueryByIDOrName(id, name)
if err != nil {
s.Ctx.WriteString(err.Error())
return
}
s.Data["json"] = sturess
s.ServeJSON()
log.Println("query student info success")
}
func (s *Student) Update() {
err := json.Unmarshal(s.Ctx.Input.RequestBody, modules.StuRes)
if err != nil {
log.Println("json Unmarshal err", err)
s.Ctx.WriteString(errors.New("json Unmarshal err").Error())
return
}
// Judge whether students exist
ok := database.IsExits(modules.StuRes.ID)
if ok {
err = database.UpdateStu(modules.StuRes)
if err != nil {
s.Ctx.Output.IsServerError()
log.Println("update student info err", err)
s.Ctx.WriteString(errors.New("update student info failed").Error())
return
}
} else {
s.Ctx.WriteString(errors.New("student not exists").Error())
return
}
s.Ctx.WriteString("update ok")
log.Println("update student info success")
}
func (s *Student) Count() {
count, err := database.Count(modules.StuRes)
if err != nil {
s.Ctx.WriteString(err.Error())
return
}
s.Data["json"] = count
s.ServeJSON()
s.Ctx.WriteString("count ok")
log.Println("count student info success")
}
5.2 database
package database
import (
"student/modules"
"student/common"
)
func InsertStu(stures *modules.StudentRes) error {
_, err := common.Engine.Table("t_student").InsertOne(stures)
if err != nil {
return err
}
return nil
}
// Look up all the information
func ShowStu(stures *modules.StudentRes) ([]modules.StudentRes, error) {
docList := make([]modules.StudentRes, 0)
err := common.Engine.Table("t_student").Find(&docList)
if err != nil {
return nil, err
}
return docList, nil
}
// Delete student information
func DeleteStu(id int) error {
_, err := common.Engine.Table("t_student").Where("id = ?", id).Delete(modules.StudentRes{
})
if err != nil {
return err
}
return nil
}
// Test for presence
func IsExits(id int) bool {
var stu modules.StudentRes
ok, err := common.Engine.Table("t_student").Where("id = ?", id).Get(&stu)
if ok && err == nil {
return true
}
return false
}
// according to id perhaps name Inquire about
func QueryByIDOrName(id int, name string) ([]modules.StudentRes, error) {
docList := make([]modules.StudentRes, 0)
if id != 0 && name == "" {
err := common.Engine.Table("t_student").Where("id = ?", id).Find(&docList)
if err != nil {
return nil, err
}
return docList, nil
}
if name != "" && id == 0 {
err := common.Engine.Table("t_student").Where("name = ?", name).Find(&docList)
if err != nil {
return nil, err
}
return docList, nil
}
if id != 0 && name != "" {
err := common.Engine.Table("t_student").Where("id = ? and name = ?", id, name).Find(&docList)
if err != nil {
return nil, err
}
return docList, nil
}
return nil, nil
}
// Update student information
func UpdateStu(stures *modules.StudentRes) error {
_, err := common.Engine.Table("t_student").Update(stures)
if err != nil {
return err
}
return nil
}
func Count(stures *modules.StudentRes) (int, error) {
count, err := common.Engine.Table("t_student").Count()
if err != nil {
return 0, nil
}
return int(count), nil
}
6、 ... and 、 Source download
I put the source code in my cdn Yes , Click to download
http://qiniu.yunxue521.top/student.zip
版权声明
本文为[Chen XiaoC]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204211417593503.html
边栏推荐
- 【Groovy】MOP 元对象协议与元编程 ( 使用 Groovy 元编程进行函数拦截 | 动态拦截函数 | 动态获取 MetaClass 中的方法 | evaluate 方法执行Groovy脚本 )
- 从源码里的一个注释,我追溯到了12年前,有点意思
- 面了个腾讯拿 38K 出来的,让我见识到了基础的天花板
- 利用库函数qsort()来进行排序,实现及原理分析
- Bug custom type
- Go's relevant operation and arrangement of documents (continuous update)
- 顺序表--链表实现
- I took out 38K from Tencent and showed me the basic ceiling
- Insect stack
- web自动化总卡在文件上传和弹框处理上?
猜你喜欢
随机推荐
53.最大子数组和
源码级别的广播与监听实现
【错误记录】Groovy工程中的文件查找策略 ( main 函数中需要使用 src/main/groovy/Script.groovy | Groovy 脚本直接使用代码相对路径 )
C语言实现三子棋
Insect stack
虫子 栈
虫子 二叉树
无人驾驶虚拟仿真(十三)--图像处理之交通标志牌识别1
初学者交换两个变量的值的三种方法
53. Maximum subarray and
MySQL kills 16 questions. How long can you hold on to it?
Personal summary of three simple sorting for beginners
两天两夜,1M图片优化到100kb
Tencent side 2: what is the semi synchronization of MySQL?
SQL Server 批处理变量定义和赋值
阿里月薪15k的测试岗,面试原来这么简单
Bug talking about function stack frame
虫子 顺序表
Bug file operation
In depth analysis of TCP three handshakes, the interviewer applauded









