早上给pmo的增量数据,对方现在想要整个工程的总代码数,可以用golang找出换行数,我的工程路径为:

E:\GitHub\launchersrv2

代码实现:

// a simple go program for computing total line of souce files stored in one dir  
package main  
  
import (  
    "fmt"  
    "bufio"  
    "os"  
    "sync"  
    "strings"  
)  
  
var (  
    linesum int  
    mutex   *sync.Mutex = new(sync.Mutex)  
)  
  
var (  
    // the dir where souce file stored  
    rootPath string = "E:/GitHub/launchersrv2"  
    // exclude these sub dirs  
    nodirs [5]string = [...]string{"/bitbucket.org", "/github.com", "/goplayer", "/uniqush", "/code.google.com"}  
    // the suffix name you care  
    suffixname string = ".go"  
)  
  
func main() {  
    argsLen := len(os.Args)  
    fmt.Println("argsLen:", argsLen) 
    if argsLen == 2 {  
        rootPath = os.Args[1]  
    } else if argsLen == 3 {  
        rootPath = os.Args[1]  
        suffixname = os.Args[2]  
    }  
    // sync chan using for waiting  
    done := make(chan bool)  
    go codeLineSum(rootPath, done)  
    <-done  
  
    fmt.Println("total line:", linesum)  
}  
  
// compute souce file line number  
func codeLineSum(root string, done chan bool) {  
    var goes int              // children goroutines number  
    godone := make(chan bool) // sync chan using for waiting all his children goroutines finished  
    isDstDir := checkDir(root)  
    defer func() {  
        if pan := recover(); pan != nil {  
            fmt.Printf("root: %s, panic:%#v\n", root, pan)  
        }  
  
        // waiting for his children done  
        for i := 0; i < goes; i++ {  
            <-godone  
        }  
  
        // this goroutine done, notify his parent  
        done <- true  
    }()  
    if !isDstDir {  
        return  
    }  
  
    rootfi, err := os.Lstat(root)  
    checkerr(err)  
  
    rootdir, err := os.Open(root)  
    checkerr(err)  
    defer rootdir.Close()  
  
    if rootfi.IsDir() {  
        fis, err := rootdir.Readdir(0)  
        checkerr(err)  
        for _, fi := range fis {  
            if strings.HasPrefix(fi.Name(), ".") {  
                continue  
            }  
            goes++  
            if fi.IsDir() {  
                go codeLineSum(root+"/"+fi.Name(), godone)  
            } else {  
                go readfile(root+"/"+fi.Name(), godone)  
            }  
        }  
    } else {  
        goes = 1 // if rootfi is a file, current goroutine has only one child  
        go readfile(root, godone)  
    }  
}  
  
func readfile(filename string, done chan bool) {  
    var line int  
    isDstFile := strings.HasSuffix(filename, suffixname)  
    defer func() {  
        if pan := recover(); pan != nil {  
            fmt.Printf("filename: %s, panic:%#v\n", filename, pan)  
        }  
        if isDstFile {  
            addLineNum(line)  
            fmt.Printf("file %s complete, line = %d\n", filename, line)  
        }  
        // this goroutine done, notify his parent  
        done <- true  
    }()  
    if !isDstFile {  
        return  
    }  
  
    file, err := os.Open(filename)  
    checkerr(err)  
    defer file.Close()  
  
    reader := bufio.NewReader(file)  
    for {  
        _, isPrefix, err := reader.ReadLine()  
        if err != nil {  
            break  
        }  
        if !isPrefix {  
            line++  
        }  
    }  
}  
  
// check whether this dir is the dest dir  
func checkDir(dirpath string) bool {  
    for _, dir := range nodirs {  
        if rootPath+dir == dirpath {  
            return false  
        }  
    }  
    return true  
}  
  
func addLineNum(num int) {  
    mutex.Lock()  

    defer mutex.Unlock()  
    linesum += num  
}  
  
// if error happened, throw a panic, and the panic will be recover in defer function  
func checkerr(err error) {  
    if err != nil {  
        panic(err.Error())  
    }  
}  

将代码copy到新建的文件 statics.go

在cmd命令行:go run statics.go

可以看到输出,包括vendor下面的引用包都已经统计进去了:

file E:/GitHub/launchersrv2/vendor/qiniupkg.com/api.v7/kodo/main.go complete, line = 143
file E:/GitHub/launchersrv2/vendor/qiniupkg.com/api.v7/kodo/main_test.go complete, line = 151
file E:/GitHub/launchersrv2/vendor/qiniupkg.com/api.v7/kodo/token_test.go complete, line = 70
file E:/GitHub/launchersrv2/vendor/qiniupkg.com/api.v7/kodo/token.go complete, line = 118
file E:/GitHub/launchersrv2/vendor/qiniupkg.com/api.v7/kodo/upload.go complete, line = 180
file E:/GitHub/launchersrv2/vendor/qiniupkg.com/x/url.v7/urlescape.go complete, line = 208
file E:/GitHub/launchersrv2/vendor/qiniupkg.com/x/ctype.v7/ctype_test.go complete, line = 72
file E:/GitHub/launchersrv2/vendor/qiniupkg.com/x/rpc.v7/gob/gobrpc_client.go complete, line = 107
file E:/GitHub/launchersrv2/vendor/qiniupkg.com/x/rpc.v7/rpc_client_test.go complete, line = 66
file E:/GitHub/launchersrv2/vendor/qiniupkg.com/x/log.v7/logext_test.go complete, line = 79
file E:/GitHub/launchersrv2/vendor/qiniupkg.com/x/reqid.v7/reqid.go complete, line = 52
file E:/GitHub/launchersrv2/vendor/qiniupkg.com/x/errors.v7/error_info.go complete, line = 146
file E:/GitHub/launchersrv2/vendor/qiniupkg.com/api.v7/kodo/bucket.go complete, line = 293
file E:/GitHub/launchersrv2/vendor/qiniupkg.com/x/ctype.v7/ctype.go complete, line = 237
file E:/GitHub/launchersrv2/vendor/qiniupkg.com/x/log.v7/logext.go complete, line = 521
file E:/GitHub/launchersrv2/vendor/qiniupkg.com/x/xlog.v7/xlog.go complete, line = 211
file E:/GitHub/launchersrv2/vendor/qiniupkg.com/x/rpc.v7/rpc_client.go complete, line = 344
file E:/GitHub/launchersrv2/vendor/qiniupkg.com/x/bytes.v7/bytes.go complete, line = 177
file E:/GitHub/launchersrv2/vendor/qiniupkg.com/x/bytes.v7/bytes_test.go complete, line = 60
file E:/GitHub/launchersrv2/vendor/qiniupkg.com/x/bytes.v7/doc.go complete, line = 34
file E:/GitHub/launchersrv2/vendor/qiniupkg.com/x/bytes.v7/replace_test.go complete, line = 54
file E:/GitHub/launchersrv2/vendor/qiniupkg.com/x/bytes.v7/replace.go complete, line = 54
file E:/GitHub/launchersrv2/vendor/qiniupkg.com/x/bytes.v7/seekable/seekable_test.go complete, line = 43
file E:/GitHub/launchersrv2/vendor/qiniupkg.com/x/bytes.v7/seekable/seekable.go complete, line = 63
total line: 110393

 

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐