This is a creation in Article, where the information may have evolved or changed.
has been playing WSH, was said to be forced to keep up with the times, and it is said that go is about to release version 1.5 resolves the GC stop-the-world problem, so UMU intends to use it to develop tools later.
Recently want to count the number of lines of code, previously written with VBS can not find, directly with Golang wrote a, basic from zero began with about 4 hours to complete. Let's talk about the experience.
1, Golang Runtime incredibly did not set, can only use map instead of, initially feel not elegant, but want to also almost, do not care so much.
2, grammar is really very concise, the first time practiced hand on the feeling of learning this language, in fact, is learning its norms, language itself is too easy.
3, defer is very useful, concise, worry, such as the core function:
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}
4, Liteide X is also relatively easy to use, write import, will automatically arrange the name of the library alphabetically, so as to save the tangled order ...
Just contact, just say here, the last attached code.
UMU @ 2015-08-17 11:30//last update:2015-08-17 17:01package mainimport ("Bufio" "FMT" "OS" "Path" "Path/filepath") func I Ssourcefile (ext string) bool {var ksourcefileexts = []string{". C", ". CC", ". cpp", ". H", ". hpp", ". Go", ". Java"}for _, R: = r Ange 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}files: = Make (Map[string]bool) 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.P Rintln (file, L)}fmt. PRINTLN ("Total lines:", lines)}func adddirectory (name string, files Map[string]bool) {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}) Return}func AddFile (name string, files Map[string]bool) {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] = truefmt. 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}