go语言gin框架的基本使用

1.首先在linux环境上安装go环境,这个网上搜搜就行

2.初始化一个go mod,网上搜搜怎么初始化

3.下面go代码的网址和端口绑定自己本机的就行

4.与另一篇CSDN一起食用,效果更好哟---> libcurl的get、post的使用-CSDN博客

package main

import (
    "github.com/gin-gonic/gin"
	"flag"
	"fmt"
)

func downFile(c *gin.Context, filePath string, filename string)  {

    c.Writer.Header().Add("Content-Disposition", fmt.Sprintf("attachment; filename=%s", filename))
    c.Writer.Header().Add("Content-Type", "application/octet-stream")
    fmt.Println(filePath)
    c.File(filePath)
}


// go build -o go_gin main.go
func main() {
	var downPath *string = flag.String("down", "/tmp", "the down dir")
	flag.Parse()
	fmt.Printf("downPath = %s\n", *downPath)
	r := gin.Default()
    r.GET("/", func(c *gin.Context) {
		c.JSON(200, gin.H{
			"message": "Hello World!",
		})
	})
    
    r.GET("/ping", func(c *gin.Context) {
        c.JSON(200, gin.H{
            "message": "pong",
        })
    })
    
    r.POST("/submit", func(c *gin.Context) {
		var json struct {
			Name string `json:"name"`
			Email string `json:"email"`
		}

		if err := c.Bind(&json); err == nil {
            print("name = " + json.Name)
            print("email = " + json.Email)
			c.JSON(200, gin.H{
				"message": "JSON received",
				"name":    json.Name,
				"email":   json.Email,
			})
		} else {
			c.JSON(400, gin.H{"error": err.Error()})
		}
	})
	// 文件下载
	r.POST("/down_file", func(c *gin.Context) {
		var json struct {
			FileName string `json:"file_name"`
		}
		if err := c.Bind(&json); err == nil {
			filePath := (*downPath) + "/" + json.FileName
			downFile(c, filePath, json.FileName)
		}
		
	})
    
    r.Run("10.74.37.146:9999") // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}

  • 6
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,让我们来设计一个简单的购物车项目。首先,我们需要确定项目的功能和需求。 需求: 1. 用户可以注册、登录和注销。 2. 用户可以将商品添加到购物车。 3. 用户可以从购物车中删除商品。 4. 用户可以查看购物车中的商品列表。 5. 用户可以结算购物车中的商品。 接下来,我们可以开始编写代码了。首先,我们需要安装gin框架,可以通过以下命令来安装: ``` go get -u github.com/gin-gonic/gin ``` 接下来,我们可以创建一个main.go文件,并导入gin框架: ```go package main import ( "github.com/gin-gonic/gin" ) func main() { router := gin.Default() router.Run(":8080") } ``` 现在我们可以在浏览器中打开http://localhost:8080/,应该可以看到Gin的欢迎页面。 接下来,我们可以开始编写购物车项目的代码。首先,我们需要创建一个路由来处理用户的注册、登录和注销功能: ```go package main import ( "github.com/gin-gonic/gin" ) func main() { router := gin.Default() // 注册 router.POST("/register", func(c *gin.Context) { // TODO: 处理注册逻辑 }) // 登录 router.POST("/login", func(c *gin.Context) { // TODO: 处理登录逻辑 }) // 注销 router.POST("/logout", func(c *gin.Context) { // TODO: 处理注销逻辑 }) router.Run(":8080") } ``` 接下来,我们需要创建一个商品模型,用于表示商品的信息: ```go type Product struct { ID int `json:"id"` Name string `json:"name"` Price int `json:"price"` } ``` 然后,我们需要创建一个全局的商品列表,用于存储所有的商品信息: ```go var products = []Product{ {ID: 1, Name: "iPhone", Price: 8888}, {ID: 2, Name: "iPad", Price: 5888}, {ID: 3, Name: "MacBook", Price: 12888}, } ``` 现在,我们可以创建一个路由来处理商品的添加和删除功能: ```go // 添加商品到购物车 router.POST("/cart", func(c *gin.Context) { // 获取商品ID id, err := strconv.Atoi(c.PostForm("id")) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid product ID"}) return } // 查找商品 var product *Product for i := range products { if products[i].ID == id { product = &products[i] break } } // 商品不存在 if product == nil { c.JSON(http.StatusBadRequest, gin.H{"error": "Product not found"}) return } // TODO: 将商品添加到购物车 c.JSON(http.StatusOK, gin.H{"message": "Product added to cart"}) }) // 从购物车中删除商品 router.DELETE("/cart/:id", func(c *gin.Context) { // 获取商品ID id, err := strconv.Atoi(c.Param("id")) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid product ID"}) return } // TODO: 从购物车中删除商品 c.JSON(http.StatusOK, gin.H{"message": "Product removed from cart"}) }) ``` 最后,我们可以创建一个路由来展示购物车中的商品列表,并提供结算功能: ```go // 查看购物车中的商品列表 router.GET("/cart", func(c *gin.Context) { // TODO: 获取购物车中的商品列表 c.JSON(http.StatusOK, gin.H{"cart": "TODO"}) }) // 结算购物车中的商品 router.POST("/checkout", func(c *gin.Context) { // TODO: 处理结算逻辑 c.JSON(http.StatusOK, gin.H{"message": "Checkout successfully"}) }) ``` 至此,我们完成了购物车项目的基本功能。完整代码如下: ```go package main import ( "github.com/gin-gonic/gin" "net/http" "strconv" ) type Product struct { ID int `json:"id"` Name string `json:"name"` Price int `json:"price"` } var products = []Product{ {ID: 1, Name: "iPhone", Price: 8888}, {ID: 2, Name: "iPad", Price: 5888}, {ID: 3, Name: "MacBook", Price: 12888}, } func main() { router := gin.Default() // 注册 router.POST("/register", func(c *gin.Context) { // TODO: 处理注册逻辑 }) // 登录 router.POST("/login", func(c *gin.Context) { // TODO: 处理登录逻辑 }) // 注销 router.POST("/logout", func(c *gin.Context) { // TODO: 处理注销逻辑 }) // 添加商品到购物车 router.POST("/cart", func(c *gin.Context) { // 获取商品ID id, err := strconv.Atoi(c.PostForm("id")) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid product ID"}) return } // 查找商品 var product *Product for i := range products { if products[i].ID == id { product = &products[i] break } } // 商品不存在 if product == nil { c.JSON(http.StatusBadRequest, gin.H{"error": "Product not found"}) return } // TODO: 将商品添加到购物车 c.JSON(http.StatusOK, gin.H{"message": "Product added to cart"}) }) // 从购物车中删除商品 router.DELETE("/cart/:id", func(c *gin.Context) { // 获取商品ID id, err := strconv.Atoi(c.Param("id")) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid product ID"}) return } // TODO: 从购物车中删除商品 c.JSON(http.StatusOK, gin.H{"message": "Product removed from cart"}) }) // 查看购物车中的商品列表 router.GET("/cart", func(c *gin.Context) { // TODO: 获取购物车中的商品列表 c.JSON(http.StatusOK, gin.H{"cart": "TODO"}) }) // 结算购物车中的商品 router.POST("/checkout", func(c *gin.Context) { // TODO: 处理结算逻辑 c.JSON(http.StatusOK, gin.H{"message": "Checkout successfully"}) }) router.Run(":8080") } ``` 当然,这只是一个简单的示例项目,实际的购物车项目可能涉及到更多的功能和复杂的业务逻辑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值