Wrote a computer search file in the Go language small thing

Source: Internet
Author: User

Package Mainimport ("bytes" "FMT" "OS" "Os/exec" "Path/filepath" "Sync") type Jieguo struct {go_num intlock sync. Mutex}var searchname string = os. Args[1]var Search_result Chan string = Make (Chan string, 1) var nums chan int = make (chan int, 1) var x jieguo = Jieguo{go_n Um:0}func Main () {list: = []string{' D: ', ' E: ', ' F '}for _, I: = Range list {fmt. Println (i, searchname) go pan (i)}for {select {case x: = <-search_result:fmt. PRINTLN (x) case y: = <-nums:if y = = 0 {os. Exit (0)}}}}func pan (path string) {A: = Exec.command ("ls", path) result, E: = A.output () if E! = nil {fmt. Println (e)}z: = bytes. Split (result, []byte{10}) for _, I: = Range Z[:len (z)-1] {s: = path + "\ \" + String (i) fmt. PRINTLN (s) e = os. Chdir (s) if E = = nil {X.lock.lock () X.go_num = X.go_num + 1x. Lock.unlock () go func () {filepath. Walk (S, Walk) x.lock.lock () X.go_num = X.go_num-1nums <-x.go_numx. Lock.unlock ()} ()} else {fmt. PRINTLN (e) if string (i) = = Searchname {fmt. Println (s)}}}}func Walk (path string, info OS. FileInfo, err Error) error {If err! = Nil {return err}if!info. Isdir () && info. Name () = = searchname {search_result <-pathreturn nil}return Nil}

Wrote a computer search file in the Go language small thing

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.