gin go并发案例

通过管道进行处理机制

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并发案例)