package eventServer import ( "math" "sync" ) var LhChan = make(chan [2]float64, 20) //传长,高 var WChan = make(chan float64, 20) //传宽 func CalcCap() (l, w, h float64) { var wg sync.WaitGroup wg.Add(2) go func() { lh := <-LhChan l = lh[0] h = lh[1] wg.Done() }() go func() { w = <-WChan wg.Done() }() wg.Wait() return } type Constant struct { Length float64 Width float64 Height float64 Volume float64 } func (c Constant) Calc() float64 { return c.Length * c.Width * c.Height } func FindBestMatch(constants []Constant, variable Constant) Constant { bestMatch := constants[0] bestMatchScore := CalculateMatchScore(bestMatch, variable) for i := 1; i < len(constants); i++ { score := CalculateMatchScore(constants[i], variable) if score < bestMatchScore || (score == bestMatchScore && constants[i].Length > bestMatch.Length) || (score == bestMatchScore && constants[i].Length == bestMatch.Length && constants[i].Height > bestMatch.Height) || (score == bestMatchScore && constants[i].Length == bestMatch.Length && constants[i].Height == bestMatch.Height && constants[i].Width > bestMatch.Width) { bestMatch = constants[i] bestMatchScore = score } } return bestMatch } func CalculateMatchScore(constant, variable Constant) float64 { //lengthScore := math.Abs(float64(constant.Length - variable.Length)) //widthScore := math.Abs(float64(constant.Width - variable.Width)) //heightScore := math.Abs(float64(constant.Height - variable.Height)) return math.Abs(constant.Calc() - variable.Calc()) }