Go Language Programming study (eight)

Source: Internet
Author: User

One, filter

Package Mainimport ("Flag"    "FMT"    "Log"    "OS"    "Path/filepath"    "Runtime"    "Strings") Func main () {runtime. Gomaxprocs (runtime. NUMCPU ())//Use all the machine ' s coresLog. SetFlags (0) algorithm, minSize, maxSize, suffixes, files:=Handlecommandline ()ifAlgorithm = =1{sink (filtersize (MinSize, maxSize, filtersuffixes (suffixes, source (files) ))}Else{channel1:=source (Files) Channel2:=filtersuffixes (suffixes, channel1) Channel3:=filtersize (minSize, maxSize, Channel2) sink (CHANNEL3)}} Func handlecommandline () (Algorithmint, MinSize, maxSize int64, suffixes, files []string) {flag. Intvar (&algorithm,"algorithm",1,"1 or 2") flag. Int64var (&minsize,"min", -1,        "Minimum file size ( -1 means no minimum)") flag. Int64var (&maxsize,"Max", -1,        "Maximum file size ( -1 means no maximum)")    varSuffixesopt *string= Flag. String ("suffixes","",        "comma-separated List of file suffixes") flag. Parse ()ifAlgorithm! =1&& Algorithm! =2{algorithm=1    }    ifMinSize > MaxSize && maxSize! =-1{log. Fatalln ("minimum size must be < maximum size")} suffixes= []string{}    if*suffixesopt! =""{suffixes= Strings. Split (*suffixesopt,",")} Files=flag. Args ()returnalgorithm, minSize, maxSize, suffixes, files}func source (files []string) <-chanstring {     out: = Make (chanstring, +) go func () { for_, FileName: =Range Files { out<-filename} close ( out)    }()    return  out}//Make the buffer the same size as for files to maximize throughputFunc filtersuffixes (suffixes []string,inch<-chanstring) <-chanstring {     out: = Make (chanstring, Cap (inch) ) go func () { forFileName: = Rangeinch {            ifLen (suffixes) = =0 {                 out<-filenameContinue} ext:=strings. ToLower (filepath. EXT (filename)) for_, Suffix: =Range Suffixes {ifext = =suffix { out<-filename Break}}} close ( out)    }()    return  out}//Make the buffer the same size as for files to maximize throughputFunc filtersize (minimum, maximum int64,inch<-chanstring) <-chanstring {     out: = Make (chanstring, Cap (inch) ) go func () { forFileName: = Rangeinch {            ifMinimum = =-1&& Maximum = =-1 {                 out<-filename//don ' t does a stat call it 's not needed                Continue} finfo, err:=OS. Stat (filename)ifErr! =Nil {Continue //Ignore files We can ' t process} Size:=Finfo. Size ()if(Minimum = =-1|| Minimum >-1&& minimum <= size) &&(Maximum== -1|| Maximum >-1&& Maximum >=size) {                 out<-filename}} Close ( out)    }()    return  out}func Sink (inch<-chanstring) {     forFileName: = Rangeinch{fmt. PRINTLN (filename)}}

This filter is a very simple and large function, set some parameters can be. Implementation is the Unix-like pipeline, which is serial.

First, the flag packet was used to parse the command-line arguments, including the suffix, size range, and file name range, and then sequentially set up 3 channel.

The first channel to the write end is to parse the file name list into one of the file names.

The second channel's write end is the result of filtering the first file name by suffix.

The writing end of the third channel is the result of filtering the second file name by size range.

After the last sink reads the third channel, it prints out the results.

Well.. Different parameters, one branch is filtered only once.

About Flag packages. Searched, parsing parameters when the time is this: first, there are two definitions of parameters, one is with-symbol of one is no-symbol. The method of flag is parsed out with a-sign, and the args method of flag is to get other parameters. It seems that nothing else is too special ~

As for the channel and the Goroutin, it seems there is nothing special to explain.

Go Language Programming study (eight)

Related Article

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.