As early as November 2009 when Google launched the Go language, it was driven by curiosity to download the experience. The feeling at the time was that the syntax was rather strange, the resources associated were less, and the dynamic language Python was used. Although it claims to be a language for concurrency, it feels that Python has been able to meet its daily development requirements, so it doesn't feel like it has any advantages.
But it is also continuing to focus on its 1.1 point development: More and more projects are being developed using go as a development language, and more and more programmers, including Python programmers, are using the go language. Now, go1.4 has supported the development of Android, have time to take the go out of the trial, the original in order to pursue the grammatical simplicity of the more eccentric grammar became less uncomfortable, and began to appreciate its many advantages, the following is my summary of the advantages of go:
1, go and the C + +, is the code can be compiled directly into a binary executable program, but unlike C + + is the go with the garbage collector, which can not lose too much performance of the case greatly simplify the design of the program.
2, go and Python-like built-in powerful and easy to use data structure
3. Rich Library and detailed development documents: a language is not easy to use, in addition to the language itself, but also to provide a powerful library support, a good community and the community to maintain a wealth of documents. In this regard, the individual thinks go is doing quite well, the document is written in detail, and a lot of the sample code is available.
4, greatly simplifies the development of concurrent programs, using channel to develop concurrent programs is really very convenient
5, concise grammar, minimize the burden of programmer typing, improve development efficiency.
And then look at the comparison between go and Python that I summed up:
1, in fact, some of the design philosophy of Go and Python is the same, like its short name, go pursuit of the code is concise, even to some harsh point.
2, not only in the syntax, go to minimize unnecessary key operation, its garbage collection mechanism, its ability of concurrent programming, undoubtedly greatly facilitates the development of programmers.
3, careful analysis of go and Python also have similarities, such as the range function, such as filepath. Walk and so on.
4, although the developer did not explicitly, but certainly go is to learn from the Python experience in the ease of use, which is why many Python programmers turn to go for reasons.
Next look at the relevant resources for go:
1. Official Network: http://golang.org/
2. Official go language Getting Started Guide: HTTP://TOUR.GOLANG.ORG/WELCOME/1
This guide takes a detailed example to guide you through learning and understanding the features of the go language, and there is no better introductory learning resource for go. These examples are divided into three sections:
1), Base: The presenter is some basic concepts of go, flow control statements and some built-in data structures: use of slice and map
2), Next Talk Method (Methods) and interface (interfaces)
3), followed by concurrency (concurrency)
These examples need some patience to be familiar with, especially interface and concurrency need some time to digest when they first touch. When you get familiar with all the examples, go is a starter.
3. Code path: Https://github.com/golang/go
It's probably too early for beginners to have a look at the compiled code or the go code to download it.
4. Development document: http://golang.org/pkg/
Needless to say, go developers must.
And then pull it down and look at the online collection of some common examples:
1. Go language for HTTP sharing
Package Mainimport ( "net/http" " os" " Strings") func Sharedir (dirName string,port string,ch chan bool) { h: = http. Fileserver (http. Dir (dirName)) err: = http. Listenandserve (":" +port,h) if err! = Nil { println ("Listenandserve:", err. Error ()) ch <-false }}func main () { ch: = Make (Chanbool) Port: = "8000"//default Port Iflen ( Os. Args) >1 { port = strings. Join (OS. Args[1:2], "") } Go Sharedir (".", Port,ch) println ("Listening on port", "Port," ... ") bresult: = <-ch Iffalse = = bresult { println ("Listening on port", Port, "failed") }}
Links: http://www.cnblogs.com/MikeZhang/archive/2012/03/13/httpShareGolang20120312.html
2, Go file and directory traversal method-Path/filepath. Walk
Package Mainimport ( "Path/filepath" " os" " fmt" " flag") Func Walkfunc (path string, info OS. FileInfo, err Error) error { return nil}type Walker struct { directories []string files []string}func Main () { flag. Parse ()// root: = flag. ARG (0) Walker: = new (Walker) path: = "." FilePath. Walk (Path, func (path string, info OS). FileInfo, err Error) error { If err! = Nil {return ERR} if info. Isdir () { walker.directories = append (walker.directories, path) } else { walker.files = append ( Walker.files, Path) } ( nil }) FMT. Printf ("Found%d dir and%d files\n", Len (walker.directories), Len (walker.files)) for I:=0;i<len ( Walker.files); i+=1 { fmt. Println (Walker.files[i]) }}
Reference Link: http://caryagos.com/press/go-walk-files-diretories/
3. Simple TCP Proxy Server
Netfwd.go
Package Mainimport ("NET" "FMT" "IO" "OS") Func main () {if Len (OS. Args)! = 3 {fatal ("USAGE:NETFWD localip:localport remoteip:remoteport")}localaddr: = OS. ARGS[1]REMOTEADDR: = os. Args[2]local, err: = Net. Listen ("TCP", localaddr) if local = = nil {fatal ("Cannot Listen:%v", err)}for {conn, err: = Local. Accept () If conn = = nil {Fatal ("Accept failed:%v", err)}go forward (conn, remoteaddr)}} func forward (local net. Conn, remoteaddr string) {remote, err: = Net. Dial ("TCP", remoteaddr) if remote = = Nil {fmt. fprintf (OS. Stderr, "Remote dial failed:%v\n", err) Return}go io. Copy (local, remote) go IO. Copy (remote, local)} func fatal (S string, a ... interface{}) {FMT. fprintf (OS. Stderr, "NETFWD:%s\n", FMT. Sprintf (S, a)) OS. Exit (2)}
4, command line parameter parsing
Go version:
Package Mainimport ( "flag" "FMT") var ( IP = flag. String ("Host", "127.0.0.1", "IP Address") port = flag. String ("Port", "8000", "Listen Port")) Func main () { flag. Parse () FMT. Println ("IP :", *ip) FMT. Println ("Port:", *port)}
Python version:
#! /usr/bin/pythonimport getopt,sysif __name__ = = "__main__": try: Opts,args = getopt.getopt (sys.argv[1:], "H:P: ", [" host= "," port= "]) except getopt. Getopterror: print "Usage:" print "-H arg,--host=arg:set IP address" print "-P arg,--port=arg:set por T " sys.exit (1) host =" 127.0.0.1 " port = 8000 for Opt,arg and opts: if opt in ("-H ","--host "): Host = arg if opt in ("-P", "--port"): port = arg print "IP :", host print "Port:", port
Reference Links:
Http://www.cnblogs.com/MikeZhang/archive/2012/09/07/argsTest20120907.html
5. Call other programs and get program output (go and Python)
Go
Package Mainimport ( "os/exec" " FMT") func main () { cmd: = Exec.command ("ls", "-l") buf, err: = cmd. Output () FMT. Printf ("%s\n%s", Buf,err)}
Python
Import Osvar = Os.popen (' ls-l '). Read () print Var
6. Go language file operation
Write a file
Package Mainimport ( "OS" "FMT") func main () { UserFile: = "test.txt" fout,err: = os. Create (userfile) defer fout. Close () if err! = Nil { FMT. Println (Userfile,err) return } for i:= 0;i<10;i++ { fout. WriteString ("Just a test!\r\n") fout. Write ([]byte ("Just a test!\r\n") }}
Read the file
Package Mainimport ( "OS" "FMT") func main () { UserFile: = "test.txt" fin,err: = os. Open (userfile) defer fin. Close () if err! = Nil { FMT. Println (Userfile,err) return } buf: = Make ([]byte, 1024x768) for{ N, _: = Fin. Read (BUF) if 0 = = n {break} os. Stdout.write (Buf[:n]) }}
deleting files
Func Remove (name string) Error
Specific See official website: http://golang.org/pkg/os/#Remove
7, Golang through the thrift Framework perfect for cross-language calls
http://my.oschina.net/u/572653/blog/165285
Finally, the "Go language father Talk go: Boulevard to Jane" to end this article:
In this article, the father of the go language declares that the goal of GO is to liberate the programmer! We have seen go in the effort to solve the programmer, but when can liberate the programmer, we still have to try to wait, oh ah.
2015,go,let ' s go.
Go language summary and learning Guide