一.同时向管道读写数据
package mainimport "fmt" func main ( ) { writeChan := make ( chan int , 20 ) exitChan := make ( chan bool ) go readData ( writeChan, exitChan) go writeData ( writeChan) for { v, ok := <- exitChan if ok { fmt. Println ( "完成:" , v) break } }
} func writeData ( writeChan chan int ) { for i := 1 ; i <= 20 ; i++ { writeChan <- i fmt. Println ( "写入数据~~:" , i) } close ( writeChan)
} func readData ( writeChan chan int , exitChan chan bool ) { for v := range writeChan { fmt. Println ( "读取数据~~:" , v) } exitChan <- true close ( exitChan)
}
二.协程案例-计算2000个数各个数的累加和
package mainimport ( "fmt" "sync"
) var ( wg3 sync. WaitGroup wg4 sync. WaitGroup
) func main ( ) { numChan := make ( chan int , 2000 ) wg3. Add ( 2000 ) go PutData ( numChan) wg3. Wait ( ) close ( numChan) fmt. Println ( "num:" , len ( numChan) ) wg4. Add ( 2000 ) resChan := make ( chan map [ int ] int , 2000 ) for n := range numChan { go SumUp ( n, resChan) } wg4. Wait ( ) close ( resChan) fmt. Println ( "res:" , len ( resChan) ) for res := range resChan { for key, val := range res { fmt. Printf ( "res[%v]=%v\n" , key, val) } }
}
func SumUp ( n int , resChan chan map [ int ] int ) { sumMap := make ( map [ int ] int ) res := 0 for i := 1 ; i <= n; i++ { res += i } sumMap[ n] = res resChan <- sumMap defer wg4. Done ( )
} func PutData ( numChan chan int ) { for i := 1 ; i <= 2000 ; i++ { numChan <- i wg3. Done ( ) }
}
三.生产1000个数据保存文件,读取文件排序后另存文件
package mainimport ( "bufio" "fmt" "io" "math/rand" "os" "strconv" "strings" "sync" "time"
) var ( wg5 sync. WaitGroup
)
func main ( ) { wg5. Add ( 1000 ) go writeDataToFile ( ) wg5. Wait ( ) fmt. Println ( "文件写入完成!!!!!!!!!!" ) fmt. Println ( ) fmt. Println ( "读取所写文件对其排序生成新的文件!!!!!!!" ) dataChan := make ( chan int , 1000 ) readDataToChannel ( dataChan) sortToSave ( dataChan)
}
func sortToSave ( dataChan chan int ) { dataSlice := make ( [ ] int , len ( dataChan) ) for i := 0 ; i < len ( dataSlice) ; i++ { for data := range dataChan { dataSlice[ i] = databreak } } fmt. Println ( len ( dataSlice) ) QuickSort ( dataSlice) fmt. Println ( "排序后:" , dataSlice) file, err := os. OpenFile ( "d:/go-test/sortData.txt" , os. O_WRONLY| os. O_TRUNC| os. O_APPEND| os. O_CREATE, 0666 ) if err != nil { fmt. Println ( "open file err " , err) return } defer file. Close ( ) writer := bufio. NewWriter ( file) for i := 0 ; i < len ( dataSlice) ; i++ { _ , err := writer. WriteString ( strconv. Itoa ( dataSlice[ i] ) + "," ) if err != nil { fmt. Println ( "write file err " , err) break } } writer. Flush ( ) fmt. Println ( "有序文件写入完成!!!!!" )
}
func writeDataToFile ( ) { rand. Seed ( time. Now ( ) . UnixNano ( ) ) file, err := os. OpenFile ( "d:/go-test/data.txt" , os. O_WRONLY| os. O_TRUNC| os. O_APPEND| os. O_CREATE, 0666 ) if err != nil { fmt. Println ( "open file err " , err) return } defer file. Close ( ) writer := bufio. NewWriter ( file) for i := 0 ; i < 1000 ; i++ { _ , err := writer. WriteString ( strconv. Itoa ( rand. Intn ( 1000 ) + 1 ) + "," ) wg5. Done ( ) if err != nil { fmt. Println ( "write file err " , err) break } } writer. Flush ( )
}
func readDataToChannel ( dataChan chan int ) { file, err := os. Open ( "d:/go-test/data.txt" ) if err != nil { fmt. Println ( "open file err " , err) return } defer file. Close ( ) reader := bufio. NewReader ( file) for { dataStr, errRead := reader. ReadString ( ',' ) if errRead != nil { if errRead == io. EOF { fmt. Println ( "读取结束!!!!" ) } else { fmt. Println ( "读取错误:" , errRead) } break } atoi, _ := strconv. Atoi ( strings. Trim ( dataStr, "," ) ) dataChan <- atoi } close ( dataChan) fmt. Println ( "长度:" , len ( dataChan) )
}
func QuickSort ( arr [ ] int ) { if len ( arr) < 2 { return } left, right := 0 , len ( arr) - 1 pivot := right for i := 0 ; i < len ( arr) ; i++ { if arr[ i] < arr[ pivot] { arr[ left] , arr[ i] = arr[ i] , arr[ left] left++ } } arr[ left] , arr[ right] = arr[ right] , arr[ left] QuickSort ( arr[ : left] ) QuickSort ( arr[ left+ 1 : ] )
}