package main
import (
"fmt"
"io/ioutil"
"strconv"
"strings"
"time"
)
const (
FILE = "D:/lite-workspace/writefile/test.txt"
)
type UserInfo struct {
id string
name string
company string
}
type ReadResult struct {
test string
//参数Channel
flag chan string
//结果Channel
result chan map[string]*UserInfo
}
//Read file
func main() {
t := time.Now().UnixNano()
// fd, err := os.Open(FILE) //打开文件
// if err != nil {
// fmt.Println("open file error")
// }
// defer fd.Close()
// len, err := fd.Seek(0, 2) //获取文件长度
// if err != nil {
// fmt.Println("get len error")
// }
// fmt.Println(len)
// strbyte := make([]byte, len)
// fd.Seek(0, 0) //移回指针到文件开头
// fd.Read(strbyte) //读文件
//fmt.Println(string(strbyte))
dat, err := ioutil.ReadFile(FILE) //直接读文件
if err != nil {
fmt.Println("Read file err")
fmt.Println(string(dat))
}
dat1, err1 := ioutil.ReadFile(FILE) //直接读文件
if err1 != nil {
fmt.Println("Read file err")
fmt.Println(string(dat1))
}
splitStr := strings.Split(string(dat), "\n")
splitStr1 := strings.Split(string(dat1), "\n")
//for user := range splitStr{
//}
//读取的数据写入map
row := len(splitStr) - 1
// countryCapitalMap := map[string]*UserInfo{}
// for i := 0; i < row; i++ {
// ///////////////
// everyUser := strings.Split(splitStr[i], "---")
// ///////////////
// var user1 UserInfo
// var user_pointer *UserInfo
// id := strings.Split(everyUser[0], ":")[1]
// user1.id = id
// user1.company = strings.Split(everyUser[1], ":")[1]
// user1.name = strings.Split(everyUser[2], ":")[1]
// user_pointer = &user1
// countryCapitalMap["key"+id] = user_pointer
// }
read1Chan := ReadResult{"1", make(chan string), make(chan map[string]*UserInfo)}
read2Chan := ReadResult{"2", make(chan string), make(chan map[string]*UserInfo)}
// strCount := len(splitStr)
// var splitStr1 = make([]string, strCount)
// copy(splitStr1, splitStr)
go read2Memory(splitStr, 0, row/2, t, read1Chan)
go read2Memory(splitStr1, row/2+1, row, t, read2Chan)
readResult1 := <-read1Chan.flag
readResult2 := <-read2Chan.flag
readResultMap1 := <-read1Chan.result
readResultMap2 := <-read2Chan.result
mapT := float64(time.Now().UnixNano()-t) / (1000000 * 1000)
fmt.Println(mapT)
fmt.Print(readResult1, readResult2)
//fmt.Println(countryCapitalMap["key10000"])
s1Chan := make(chan string)
s2Chan := make(chan string)
s3Chan := make(chan string)
s4Chan := make(chan string)
// s5Chan := make(chan string)
// s6Chan := make(chan string)
fmt.Println("-----" + strconv.Itoa(len(readResultMap1)))
fmt.Println("result.test----" + read1Chan.test)
go searchMap("244094", readResultMap1, t, s1Chan)
go searchMap("6530499", readResultMap1, t, s2Chan)
go searchMap("17201129", readResultMap2, t, s3Chan)
go searchMap("11929833", readResultMap2, t, s4Chan)
// go searchMap("7786084", countryCapitalMap, t, s5Chan)
// go searchMap("5484924", countryCapitalMap, t, s6Chan)
searchT := float64(time.Now().UnixNano()-t) / (1000000 * 1000)
fmt.Println("AllSearchT----" + strconv.FormatFloat(searchT, 'f', -1, 64))
r1, r2, r3, r4 := <-s1Chan, <-s2Chan, <-s3Chan, <-s4Chan
fmt.Print(r1, r2, r3, r4)
//fmt.Println(row)
//fmt.Println(len(dat))
// 'b' (-ddddp±ddd,二进制指数)
// 'e' (-d.dddde±dd,十进制指数)
// 'E' (-d.ddddE±dd,十进制指数)
// 'f' (-ddd.dddd,没有指数)
// 'g' ('e':大指数,'f':其它情况)
// 'G' ('E':大指数,'f':其它情况)
}
func searchMap(condition string, countryCapitalMap map[string]*UserInfo, t int64, sChan chan<- string) {
condition = "eyecool" + condition
for userMap := range countryCapitalMap {
user := *countryCapitalMap[userMap]
if user.company == condition {
fmt.Println(user)
searchT := float64(time.Now().UnixNano()-t) / (1000000 * 1000)
searchTStr := strconv.FormatFloat(searchT, 'f', -1, 64)
fmt.Println(condition + "-----" + searchTStr)
sChan <- searchTStr
}
}
}
func read2Memory(splitStr []string, start int, end int, t int64, readChan ReadResult) {
var countryCapitalMap = make(map[string]*UserInfo)
for i := start; i < end; i++ {
///////////////
everyUser := strings.Split(splitStr[i], "---")
///////////////
var user1 UserInfo
var user_pointer *UserInfo
id := strings.Split(everyUser[0], ":")[1]
user1.id = id
user1.company = strings.Split(everyUser[1], ":")[1]
user1.name = strings.Split(everyUser[2], ":")[1]
user_pointer = &user1
countryCapitalMap["key"+id] = user_pointer
}
fmt.Println("countryMap:" + strconv.Itoa(len(countryCapitalMap)))
//fmt.Println(countryCapitalMap)
// fmt.Println(&(readChan.result))
readChan.result <- countryCapitalMap
readChan.test = "1111"
searchT := float64(time.Now().UnixNano()-t) / (1000000 * 1000)
searchTStr := strconv.FormatFloat(searchT, 'f', -1, 64)
fmt.Println("read" + "-----" + searchTStr)
readChan.flag <- "complete"
}
有疑问加站长微信联系(非本文作者)