Go language first taste

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.


The only thing to keep in mind when it comes to language design is that C becomes C + +.


Go is a lightweight and concise support for concurrent languages that can be used for exploratory personal projects, which is the main reason I want to learn this language. For someone with some programming experience, the way to learn a new language is to take a glance at the language features and then write a medium-sized program that uses as much of the most important features as possible.


The following program is used to calculate the size of all files or directories in a directory.

Package Mainimport ("FMT" "Time" "OS" "Log") type shortfileinfo struct {fileName stringsize int64}func (fileInfo *shortfileinfo) Desc () string {return FMT. Sprintf ("{%s:%d}", Fileinfo.filename, Fileinfo.size)}func producefiles (DirName string) []os. FileInfo {path, err: = OS. Open (dirName) if err! = Nil {log. Fatal (ERR)} defer path. Close () Fileinfos, err: = path. Readdir (0); if err! = Nil {log. Fatal (Err)} return Fileinfos;} Func procFile (FileInfo os. FileInfo, basedirname string, Channelbuffer chan shortfileinfo) {var filesize int64filename: = Fileinfo.name () if FileInfo . Isdir () {filesize = Totalfilesizeindir (FileInfo, Basedirname)} else {filesize = Fileinfo.size ()} shortfileinfo: = S Hortfileinfo{filename, Filesize};fmt. Println (time. Now (). String () + "store:" + shortfileinfo.desc ()) Channelbuffer <-shortfileinfo}func totalfilesizeindir (fileInfo os. FileInfo, basedirname String) Int64 {var filesize int64 = 0fileInfos: = Producefiles (BasedirnaMe + "\ \" + fileinfo.name ()) for _, Subfileinfo: = Range Fileinfos {if Subfileinfo.isdir () {filesize + = Totalfilesizeindir ( Subfileinfo, basedirname + "\ \" + Fileinfo.name ())} else {filesize + = Subfileinfo.size ()}}return Filesize}func sleep (ns in T) {time. Sleep (time. Duration (time. Second) *time. Duration (NS))}const (B int64 = 1KB Int64 = 1024MB Int64 = 1024*1024GB Int64 = 1024*1024*1024TB Int64 = 1024*1024*1024*1024 ) Const FORMATF string = "%8.4f" func readablesize (sizeinbytes Int64) string {switch {case B <= sizeinbytes && s Izeinbytes < Kb:return FMT. Sprintf ("%db", sizeinbytes) case KB <= sizeinbytes && sizeinbytes < Mb:return FMT. Sprintf (formatf+ "KB", Float64 (sizeinbytes)/float64 (KB)) Case MB <= sizeinbytes && sizeinbytes < Gb:return Fmt. Sprintf (formatf+ "MB", Float64 (sizeinbytes)/float64 (MB)) Case GB <= sizeinbytes && sizeinbytes < Tb:return Fmt. Sprintf (formatf+ "GB", Float64 (sizeinbytes)/float64 (GB)) Case TB <= Sizeinbytes:return FMT. Sprintf (formatf+ "TB", Float64 (sizeinbytes)/float64 (TB)) default:return "0"}}func main () {basedirname: = "C:\\users\\qin . Shuq\\desktop "FileList: = Producefiles (basedirname) FileNumber: = Len (fileList) Channelbuffer: = Make (chan        Shortfileinfo, filenumber) Fileinfomap: = Make (Map[string] int64, filenumber) for _, FileInfo: = Range FileList { Go ProcFile (FileInfo, Basedirname, Channelbuffer)} for Count: = 0; Count <= FileNumber; {select {case fileInfo: = <-channelbuffer:fmt. Println (time. Now (). String () + "Fetch:" + FILEINFO.DESC ()) Fileinfomap[fileinfo.filename] = fileinfo.sizecount++default:if Count = = Filenumb ER {close (channelbuffer)}fmt.  Println ("Waiting for data ...") Sleep (2)}}var totalsize Int64 totalsize = 0for _, FileSize: = Range Fileinfomap {totalsize + = Filesize}fmt.  Printf ("Total size in%s:%db%s", Basedirname, TotalSize, Readablesize (TotalSize));}


Related Article

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.