Google Go 编程语言



字数:0 关键词: Go语言编程 Go

The Go Programming Language Rob Pike Oct 30, 2009 Monday, November 2, 2009 Go New Experimental Concurrent Garbage-collected Systems Language Monday, November 2, 2009 Hello, world package main import "fmt" func main() { fmt.Printf("Hello, 世界\n"); } Monday, November 2, 2009 Who Robert Griesemer, Ken Thompson, and Rob Pike started the project in late 2007. By mid 2008 the language was mostly designed and the implementation (compiler, run-time) starting to work. Ian Lance Taylor and Russ Cox joined in 2008. Lots of help from many others. Monday, November 2, 2009 Why Go fast! Make programming fun again. Monday, November 2, 2009 Our changing world No new major systems language in a decade. But much has changed: - sprawling libraries & dependency chains - dominance of networking - client/server focus - massive clusters - the rise of multi-core CPUs Major systems languages were not designed with all these factors in mind. Monday, November 2, 2009 Construction speed It takes too long to build software. The tools are slow and are getting slower. Dependencies are uncontrolled. Machines have stopped getting faster. Yet software still grows and grows. If we stay as we are, before long software construction will be unbearably slow. Monday, November 2, 2009 Type system tyranny Robert Griesemer: “Clumsy type systems drive people to dynamically typed languages.” Clunky typing: Taints good idea with bad implementation. Makes programming harder (think of C's const: well-intentioned but awkward in practice). Hierarchy is too stringent: Types in large programs do not easily fall into hierarchies. Programmers spend too much time deciding tree structure and rearranging inheritance. You can be productive or safe, not both. Monday, November 2, 2009 Why a new language? These problems are endemic and linguistic. New libraries won’t help. (Adding anything is going in the wrong direction.) Need to start over, thinking about the way programs are written and constructed. Monday, November 2, 2009 A New Language Monday, November 2, 2009 Goals The efficiency of a statically-typed compiled language with the ease of programming of a dynamic language. Safety: type-safe and memory-safe. Good support for concurrency and communication. Efficient, latency-free garbage collection. High-speed compilation. Monday, November 2, 2009 As xkcd observes... The image is licensed under a Creative Commons Attribution-NonCommercial 2.5 License. Monday, November 2, 2009 Compilation demo Monday, November 2, 2009 Design principles Keep concepts orthogonal. A few orthogonal features work better than a lot of overlapping ones. Keep the grammar regular and simple. Few keywords, parsable without a symbol table. Reduce typing. Let the language work things out. No stuttering; don't want to see foo.Foo *myFoo = new foo.Foo(foo.FOO_INIT) Avoid bookkeeping. But keep things safe. Reduce typing. Keep the type system clear. No type hierarchy. Too clumsy to write code by constructing type hierarchies. It can still be object-oriented. Monday, November 2, 2009 The big picture Fundamentals: Clean, concise syntax. Lightweight type system. No implicit conversions: keep things explicit. Untyped unsized constants: no more 0x80ULL. Strict separation of interface and implementation. Run-time: Garbage collection. Strings, maps, communication channels. Concurrency. Package model: Explicit dependencies to enable faster builds. Monday, November 2, 2009 New approach: Dependencies Construction speed depends on managing dependencies. Explicit dependencies in source allow: - fast compilation - fast linking The Go compiler pulls transitive dependency type info from the object file - but only what it needs. If A.go depends on B.go depends on C.go: - compile C.go, B.go, then A.go. - to compile A.go, compiler reads B.o not C.o. At scale, this can be a huge speedup. Monday, November 2, 2009 New approach: Concurrency Go provides a way to write systems and servers as concurrent, garbage-collected processes (goroutines) with support from the language and run-time. Language takes care of goroutine management, memory management. Growing stacks, multiplexing of goroutines onto threads is done automatically. Concurrency is hard without garbage collection. Garbage collection is hard without the right language. Monday, November 2, 2009 ... quickly Monday, November 2, 2009 Basics const N = 1024 // just a number const str = “this is a 日本語 string\n” var x, y *float var ch = '\u1234' /* Define and use a type, T. */ type T struct { a, b int } var t0 *T = new(T); t1 := new(T); // type taken from expr // Control structures: // (no parens, always braces) if len(str) > 0 { ch = str[0] } Monday, November 2, 2009 Program structure package main import "os" import "flag" var nFlag = flag.Bool("n", false, `no \n`) func main() { ! flag.Parse(); ! s := ""; ! for i := 0; i < flag.NArg(); i++ { ! ! if i > 0 { s += " " } ! ! s += flag.Arg(i) !} ! if !*nFlag { s += "\n" } ! os.Stdout.WriteString(s); } Monday, November 2, 2009 Constantstype TZ int const ( UTC TZ = 0*60*60; EST TZ = -5*60*60; // and so on ) // iota enumerates: const ( bit0, mask0 uint32 = 1<



需要 8 金币 [ 分享文档获得金币 ] 0 人已下载