Go language-Parallel program WordCount

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Go in the design, there is a parallel to the syntax--channel and goroutine

The former can be very convenient to carry out messages and data transfer, when fetching data and take the data can not care about the underlying implementation, with <-to assign value

This must be Time.sleep or the program will end soon, and read and write are not even running. This is similar to Linux threading programming. I don't know if there's a better way to do it (looks like someone wrote it, and in some way tells Main to end it)

Package main import (    "FMT"    "Time" "    math/rand") var ch chan IntFunc Read () {    for{        c: = <-CH        FM T.PRINTLN ("I read", c)    }} Func write () {    for{        c: = 0        I: = Rand. Int ()%10        c + = i        fmt. Println ("I write", c)        ch <-c    }} Func main () {     ch = make (chan int, 4)    go write ()    go read () 
  
   time. Sleep (1000000)}
  

———————————————————————————————————-

WordCount code implementation, know that Hadoop should know this, parallel computing, then the go inside is also very well implemented:

Main:readfile: Read a file, perform a task distribution, distribute it to three identical compute threads, just pass the task through different Chan (the task here is to count the number of occurrences of each word in a line of characters)

Compute: Calculates the thread and sends the result to a global Chan

Redeuce: Extracts the results from the global Chan, merging the final results.

feel yourself writing the program is not very standard, many of the ideas are limited to before the idea of writing C. Here

Package Main/* #include <stdio.h> */import ("OS" "FMT" "Bufio" "Time" "Strings" "C" "Runtime") var str1 chan Stringvar str2 Chan stringvar str3 Chan stringvar keywordmap chan map[string]intvar result map[string]intfu NC wordCount (s string) Map[string]int {m:= make (map[string]int) words:=strings. Fields (s) for I:=0;i<len (words), i++ {M[words[i]] + = 1} return M} func compute (num in            T) {for {var str string if num = = 1{str = <-str1}else if num = = 2{ str = <-str2}else if num = = 3{str =<-str3}else {return} m : = WordCount (str) keywordmap <-m//fmt. Printf ("%v#", m)}}func reduce () {for {m: = <-keywordmap for key,value: = range m{FM T.println (Key,value) Result[key] + = value}}} func ReadFile () {//var content [100]byte FP, _: = OS. Open ("Wc.txt") br: = Bufio. Newreader (FP) defer FP. Close () for i:= 1;; i++ {line,err: = br. ReadString (' \ n ') if err! = nil{//return-1 Break}//fmt.            Println (line)//str1 <-line if i%3 = = 0{Str3 <-line}else If I% 3 = = 1{ STR1 <-Line}else if i%3 = = 2{str2 <-Line}}/*//T.PRINTLN (String (conte nt[:])//mystr: = String (content[:])//array: = Strings. Split (mystr, "/r/n")//fmt. Println (array[:]) */} func main () {runtime. Gomaxprocs (2) str1 = Make (Chan string, 3) str2 = Do (chan string, 3) Str3 = Make (Chan string, 3) Keywordmap = Make (chan map[string]int, 5) result = Make (Map[string]int) time. Sleep (1000000) go ReadFile ()//time. Sleep (100000000) go reduce () go compute (1) Go compute (2) Go compute (3) time. Sleep (10000000000)/* for{time. Sleep (10000000)//10ms FMT. Printf ("%v#", Result)} */Defer FMT. Printf ("End result:%v#", result)}

Tips

    • godoc-http=:8000 running local documents and accessing them through the web

Reference:

    1. Go Lang Introduction
Original Blog Address: http://www.fuxiang90.com/2012/08/go%E8%AF%AD%E8%A8%80-%E5%B9%B6%E8%A1%8C%E7%A8%8B%E5%BA%8F/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.