Use golang to calculate the number of lines of code

Source: Internet
Author: User

First, modify the conf file and configure the number of lines of code in the file with the suffix

Usage: countcodeliine filename or nothing (the directory where the executable program is located)

Functions:

1. count the number of lines of code

2. You can set the type of code to be counted based on the configuration file (CONF ).

3. count the number of lines and total number of lines of each type of file

Functions to be implemented in the future:

1. Number of comment rows

2. Comment Rate

3. CSV export Function

Source code:

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') can only be run on Linux, because the windwos linefeed is \ r \ n and Linux is \ n, so it is implemented by scan, but the scan is go1.1.

The newly added struct. You must upgrade go when using it.

Conf configuration file:

.go.html.css.js

GitHub address:

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

You are welcome to modify it at will.

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.