go+gin实现一个简单的web程序
本文参考与 https://go.dev/doc/tutorial/web-service-gin,有兴趣的伙伴可以去官网阅读一下
环境准备
- goland
- go环境
编写测试
-
打开goland创建一个project
-
创建src目录
-
在src下创建一个main.go文件
-
代码
package "main" func main() { }
-
-
在main.go中创建album结构体
-
代码
type album struct { ID string `json:"id"` Title string `json:"title"` Artist string `json:"artist"` Price float64 `json:"price"` }
-
-
在main.go中创建结构体切片
-
代码
// 专辑切片到种子唱片专辑数据。 var albums = []album{ {ID: "1", Title: "Blue Train", Artist: "John Coltrane", Price: 56.99}, {ID: "2", Title: "Jeru", Artist: "Gerry Mulligan", Price: 17.99}, {ID: "3", Title: "Sarah Vaughan and Clifford Brown", Artist: "Sarah Vaughan", Price: 39.99}, }
-
对切片进行增删改查操作
-
因为需要将切片转换为json格式,所有需要gin框架
-
这里需要配置一下goland
启用go modules 将代理设置为https://goproxy.cn这是国内的代理,不然下载依赖会很慢很有可能访问不了
勾选这两个选项
-
-
增删改查函数
-
查询
-
代码(查询所有)
func getAlbums(c *gin.Context) { c.IndentedJSON(http.StatusOK, albums) }
-
代码(根据id查询)
func getAlbumsById(c *gin.Context) { for _, albums := range albums { if albums.ID == c.Param("id") { c.IndentedJSON(http.StatusCreated, albums) return } } c.IndentedJSON(http.StatusNotFound, gin.H{"message": "this id not fount,please begin input!"}) }
-
-
增加
-
代码
func postAlbums(c *gin.Context) { var newAlbum album // Call BindJSON to bind the received JSON to // newAlbum. /* if err := c.BindJSON(&newAlbum); err != nil { fmt.Println("this is run return") return }*/ newAlbum.ID = "4" newAlbum.Title = "依然范特西" newAlbum.Artist = "jay" newAlbum.Price = 50.99 albums = append(albums, newAlbum) c.IndentedJSON(http.StatusCreated, newAlbum) }
-
-
删除
-
代码
func deleteAlbumsById(c *gin.Context) { id := c.Param("id") for index, album := range albums { if album.ID == id { albums = append(albums[:index], albums[index+1:]...) c.IndentedJSON(http.StatusCreated, albums) return } } c.IndentedJSON(http.StatusNotFound, gin.H{"message": "this id not found,please begin input!"}) }
-
-
更新
-
代码
func putAlbumsById(c *gin.Context) { params := c.Params fmt.Println(params) for index, album := range albums { if album.ID == params.ByName("id") { albums[index].Title = params.ByName("title") albums[index].Artist = params.ByName("artist") albums[index].Price, _ = strconv.ParseFloat(params.ByName("price"), 64) c.IndentedJSON(http.StatusCreated, gin.H{"message": "by id " + album.ID + " success !"}) return } } c.IndentedJSON(http.StatusNotFound, gin.H{"message": "this id not found,please begin input!"}) }
-
-
-
在main.go下启动服务器添加请求方法
-
注意需要导入的包gin包
import ( "fmt" "github.com/gin-gonic/gin" "net/http" "strconv" )
-
router := gin.Default() router.GET("/albums", getAlbums) router.POST("/albums", postAlbums) router.GET("/albums/:id", getAlbumsById) router.DELETE("/albums/:id", deleteAlbumsById) router.PUT("/albums/:id/:title/:artist/:price", putAlbumsById) err := router.Run("localhost:8080") if err != nil { return }
-
在线测试
测试工具
1.apifox
2.chrom
-
getAlbums()
-
postAlbums()
添加成功
再次查询
可以看到已经插入到切片中 -
getAlbumsById
-
deleteAlbumsById
再次查询
可以看到切片中"id"为4的元素已经被删除 -
putAlbumsById
再次查询
可以看到"id"为1的元素已经被我们修改了
总结
可以看到在go环境下开发一个web应用还是很便捷的