[UMU 學 golang](2) 升級 1.5 + 最佳化

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

  安裝 1.5 時,直接覆蓋 1.4.2,結果不能用了,報錯:imports runtime: C source files not allowed when not using cgo or SWIG

  解決方案是刪掉 go 1.5……然後修複安裝一遍。

  最佳化:1、加了計時功能,純屬蛋疼。2、學到一個不佔空間的 struct{},map[string]bool 改為 map[string]struct{}。

// UMU @ 2015-08-17 11:30// Last update: 2015-08-21 17:40package mainimport ("bufio""fmt""os""path""path/filepath""time")func isSourceFile(ext string) bool {var kSourceFileExts = []string{".c", ".cc", ".cpp", ".h", ".hpp",".go",".java"}for _, r := range kSourceFileExts {if r == ext {return true}}return false}func main() {if len(os.Args) < 2 {fmt.Println("Usage: ", os.Args[0], "[file or folder]...")return}start := time.Now()files := make(map[string]struct{})lines := 0for _, r := range os.Args {fi, err := os.Stat(r)if err == nil || os.IsExist(err) {if fi.IsDir() {AddDirectory(r, files)} else {AddFile(r, files)}}}for file, _ := range files {l := CountLine(file)lines += lfmt.Println(file, l)}fmt.Printf("Total lines: %d, cost %fs\n", lines, timeElapsed(start))}func AddDirectory(name string, files map[string]struct{}) {filepath.Walk(name, func(path string, fi os.FileInfo, err error) error {if nil == fi {return err}if fi.IsDir() {return nil}AddFile(path, files)return nil})}func AddFile(name string, files map[string]struct{}) {ext := path.Ext(name)if isSourceFile(ext) {path, err := filepath.Abs(name)if err == nil {_, exists := files[path]if exists {fmt.Println("Duplicated", path)} else {files[path] = struct{}{}fmt.Println("Add", path)}}}}func CountLine(path string) (num int) {f, err := os.Open(path)if nil != err {return}defer f.Close()s := bufio.NewScanner(f)for s.Scan() {num += 1}return}func timeElapsed(start time.Time) float64 {dis := time.Since(start).Seconds()return dis}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.