美文网首页
gin go并发案例

gin go并发案例

作者: 全栈工程师wql | 来源:发表于2019-12-26 16:43 被阅读0次

    通过管道进行处理机制

    package main

    import (

    "fmt"

    "github.com/gin-gonic/gin"

    "io/ioutil"

    "os"

    "strconv"

    )

    var sema = make(chan struct{},1)

    func main() {

    r := gin.Default()

    r.GET("/ping",func(c *gin.Context) {

    c.JSON(200, gin.H{

    "message":"pong",

    })

    })

    r.GET("/test",func(context *gin.Context) {

    file2, _ := os.OpenFile("./3.txt", os.O_RDWR|os.O_APPEND,0766)

    defer file2.Close()

    file2.WriteString("11\r\n");

    quUser()

    getU()

    context.JSON(200, gin.H{

    "message":"test",

    })

    })

    r.Run(":3300")// listen and serve on 0.0.0.0:8080

    }

    func quUser() int {

    sema <-struct{}{}

    data, err := ioutil.ReadFile("./2.txt")

    if err != nil {

    }

    balance, _ := strconv.Atoi(string(data))

    balance = balance+1

      fmt.Println(balance,"存余额\r\n")

    file, _ := os.OpenFile("./2.txt", os.O_RDWR,0766)

    defer file.Close()

    file.WriteString(strconv.Itoa(balance));

    <-sema

    return balance

    }

    func getU() {

    sema <-struct{}{}

    data, err := ioutil.ReadFile("./2.txt")

    if err != nil {

    }

    balance, _ := strconv.Atoi(string(data))

    fmt.Println(balance,"取余额\r\n")

    <-sema

    }

    相关文章

      网友评论

          本文标题:gin go并发案例

          本文链接:https://www.haomeiwen.com/subject/eflloctx.html