// main.go package main import ( "flag" "log" "os" "sort" "tickserver/server/market" ) func main() { flag.Parse() n := flag.NArg() if n != 2 { log.Println("must input 2 files to be combined", n) return } fileMain := flag.Arg(0) fileAppend := flag.Arg(1) log.Println("main:", fileMain, "append", fileAppend) candles, err := market.ReadCandleFile(fileAppend) if err != nil { log.Println(err) return } candles, err = combin(fileMain, candles) if err != nil { log.Println(err) return } w, err := os.Create(fileMain) if err != nil { log.Println(err) return } defer w.Close() err = market.ZipCBuf(w, candles) if err != nil { log.Println(err) return } } // 把之前文件中candles和新的合并 func combin(filename string, candles []market.Candle) ([]market.Candle, error) { buf, err := market.ReadCandleFile(filename) if err != nil { return candles, err } // 去除重复数据 ts := buf[len(buf)-1].Timestamp n := sort.Search(len(candles), func(i int) bool { if candles[i].Timestamp >= ts { return true } return false }) if n == len(candles) { n = 0 } if candles[n].Timestamp == ts { n += 1 } candles = append(buf, candles[n:]...) return candles, nil }