統計程式碼數工具 用golang實現

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

首先:修改conf檔案,配置要尋找什麼尾碼的檔案程式碼數

使用方法:countcodeliine filename or nothing(計算可執行程式所在目錄)


實現功能:

1,統計程式碼數

2,可以根據設定檔(conf)來進行設定要對什麼類型的代碼進行統計

3,統計每個類型檔案行數,和總行數


將來要實現功能:

1,注釋行數

2,注釋率

3,匯出csv功能


原始碼:

package mainimport("os""container/list""io""log""path/filepath""bufio")func init(){log.SetFlags(log.Llongfile| log.LstdFlags)}func main(){conf,err := ParseConf()if nil != err{log.Println(err)return}allFiles := GetAllFilesName()srcList := GetParsedFilesByConf(allFiles, conf)res := Parse(srcList)total := 0for k,v := range res{total += vprintln(k,":",v)}println("Total: ", total)}//example:map["go" or "css"]123func Parse(files list.List)(parseResult map[string]int){parseResult = map[string]int{}for e := files.Front(); nil != e; e = e.Next(){v := e.Value.(string)ext := filepath.Ext(v)parseResult[ext] += ComputeLine(v)}return}func ComputeLine(path string)(num int){f,err := os.Open(path)if nil != err{log.Println(err)return}defer f.Close()r := bufio.NewReader(f)for{_,err := r.ReadString('\n')if io.EOF == err || nil != err{break}num += 1}return}func ParseConf()(conf []string, err error){confPath,_ := os.Getwd()confPath +=  "/conf"file,err := os.Open(confPath)if nil != err{log.Println(err)return}defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan(){conf = append(conf, scanner.Text())}//log.Println(conf)return}func GetSrcFullPath()(fullPath string){args := os.Args;parameterLen := len(args)if parameterLen == 1{fullPath,_ = os.Getwd()}else if(parameterLen >= 2){fullPath = args[1]}fullPath,_ = filepath.Abs(fullPath)return}func GetParsedFilesByConf(lst list.List,conf []string)(l list.List){for e := lst.Front(); nil != e; e = e.Next(){ext := filepath.Ext(e.Value.(string))for _,extension := range conf{if ext == extension{l.PushBack(e.Value.(string))continue}}}for e := l.Front(); nil != e; e = e.Next(){println("matched string:" ,e.Value.(string))}return}func GetAllFilesName() (lst list.List){fullPath := GetSrcFullPath()filepath.Walk(fullPath,func(path string,fi os.FileInfo,err error)error{if nil == fi {return err}if fi.IsDir() {return nil}//println(fi.Name())//println(path)//name := fi.Name()lst.PushBack(path)return nil})return}

rd := bufio.NewReader(file)var line stringfor{line, err = rd.ReadString('\n')if io.EOF == err{err = nilbreak}else if nil != err{break}conf = append(conf, line[:len(line)-1])}
ReadString('\n')只能在linux上可以運行,因為windwos分行符號是\r\n,linux是\n,所以換成用Scan來實現,但是Scan是go1.1

新增的struct,大家使用時請務必注意升級go。

conf設定檔:

.go.html.css.js

github地址:

https://github.com/zoro-du/countcodeline

歡迎大家隨便修改。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.