Coder Social home page Coder Social logo

gocook's Introduction

gocook

gocook 提供一个工作池,按照传递进来的CookMethod,对唯一名称的meal进行加工并返回加工结果。主要功能是在加工过程中,如果同一个meal名称进来,那么返回的加工结果跟之前的一样。防止在高并发的情况下加工出不一样的结果。

应用场景

外部订单号同内部订单号一一对应

安装

go get github.com/qianlnk/gocook

使用

package main

import (
    "fmt"
    "math/rand"
    "strconv"
    "strings"
    "time"

    "github.com/qianlnk/gocook"
)

func main() {
    mchID := "10001"
    outTradeNo := "20170331100001"
    meal := gocook.NewMeal(mchID+outTradeNo, newInTradeNo, mchID, outTradeNo)
    inTradeNo := meal.Get()
    fmt.Println(inTradeNo)
}

//CookMethod
func newInTradeNo(args ...interface{}) interface{} {
    //TODO 查库 如果有则返回
    var res string

    for _, arg := range args {
        res += fmt.Sprintf("%v", arg)
    }

    res +newRandomString(5)
    //TODO 入库
    return res
}

func newRandomString(length int) string {
    rand.Seed(time.Now().UnixNano())
    rs := make([]string, length)
    for start := 0; start < length; start++ {
        rs = append(rs, strconv.Itoa(rand.Intn(10)))
    }

    return strings.Join(rs, "")
}

gocook's People

Watchers

qianlnk avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.