Go (programming language)

Go is a compilable programming language that supports concurrency and has automatic garbage collection.

It was developed by company Google Inc. employees. The designs are by Robert Griesemer, Rob Pike and Ken Thompson.

Go oriented syntax to the C programming language with some influence from the landlord 's family of languages ​​( Pascal, Modula and Oberon in particular ). The support for concurrency has been designed on the model introduced by Tony Hoare Communicating Sequential Processes (CSP ) and is in tradition of programming languages ​​Newsqueak, Alef and Limbo.

Backgrounds, development goals

Go was out of dissatisfaction with the existing languages ​​for software development such as C or Java in the context of today's computer systems, particularly in terms of scalable network services, cluster and cloud computing developed. Important goals in the development included the support of concurrency with native language elements and to facilitate the software development with large teams of developers and large code bases. Go has its own garbage collector, allowing the use of pointers, but dispenses with pointer arithmetic. Go is a compiled language, was set at the value to a high translation speed.

Characteristics, language resources

Go offers Closures and reflection as well as type safety and garbage collection. Object orientation supports Go through interfaces and mixins. On classes are deliberately avoided. It is also possible to modularize the source code such as Java with by packets. Concurrency is realized by Communicating Sequential Processes that Goroutinen are called and can communicate with each other via channels ( Channels). Generic types do not exist, a subsequent integration is not excluded. Unicode is supported in the form of UTF -8, both for strings as well as for variable identifiers in the source code (but only Unicode letters and digits), " t = t1 - t2" is possible.

Syntax

The syntax of Go is essentially based on the syntax of the C programming language, but deviates from it in some places. For example, to dispense with the completion of statements with a semicolon. Types of data are written in the identifier with declarations place before, in order to simplify the declaration of function types. Code blocks are delimited by curly braces. In addition to the simple equal sign as an assignment operator, there is also the: = operator, combined with the declaration type inference and assignment. The language includes 25 keywords fewer keywords as ANSI C.

Comments are marked with slashes as in C or C; "/ * " To " * /" denotes a comment field, which can also include multiple lines, "/ /" denotes a comment field to the end of the line.

Each source file is owned by exactly one package, which is stated at the beginning of the file with the "package " statement.

The keyword for functions is " func", the function "main" in the "main " package is the starting point of the Go program.

Each variable has a defined type.

Simple example with text output

Package main   import " fmt "   func main () { fmt.Println ("Hello, World" ) } Another example calculates the circle of pi approximation of the Leibniz series. For the calculation Goroutinen and channels are used:

Package main   import ( " fmt " " math" )   func main () { fmt.Println ( calcpi (5000) ) }   / / Calcpi starts n Goroutinen a / / Approximation of pi to calculate. calcpi func ( int n ) { float64 ch: = make ( chan float64 ) for k: = 0; k < = n; k { go term (ch, float64 (k)) } f: = float64 (0.0 ) for k: = 0; k < = n; k { f = < -ch } return f }   func term (ch chan float64, float64 k ) { CH <- 4 * Math.Pow (-1, k) / (2 * k 1) } object orientation

Go supports object-oriented programming, but this is not class-based. Data types can have methods in Go. Polymorphism is achieved through interfaces (interfaces ) are linked via the method calls at runtime to the concrete implementation (dynamic binding). For a data type does not have to be explicitly declared that it meets a particular interface. This relationship is instead determined implicitly at compile time in order to achieve loose coupling.

Instead of inheritance and type hierarchies comes in Go composition for use. For this, Go supports a form of mixins, which in Go embedding ( " embedding " ) is called: a data structure can embed any number of other data types, so that they receive their methods and data fields.

Concurrency

To support concurrent programming in Go the concept of channels ( channels) is used, which provides a relatively abstract possibility of synchronous or asynchronous communication between Go- routines. A channel is a memory area that is protected by semaphores, and a queue ( buffered / asynchronous channel) or merely an interface ( unbuffered / synchronous channel) provides. About a channel can thereby transmit only data of a fixed type. Here, however, no limit is given in terms of the type, channels for channels are conceivable.

A channel is make by calling (chan type) (synchronous ) or make ( chan type, size ) ( asynchronously when size > 0 ) is created. Then go routines can be written to the channel, read from it and close it.

In synchronous channels blocked a read access until another go- routine writes in the channel or the write access by another routine reads. For asynchronous channels such behavior occurs only when the channel to be read or to be written is empty channel is full. There is no limit to the number Go to Go- routines that read and write a channel. Despite the sophisticated synchronization mechanisms can occur with the use of channels a deadlock, which causes the Go runtime to terminate the program. A Go- routine can listen via the select construct on multiple channels simultaneously, or try to write in multiple channels, the case statement is executed, which is no longer blocked first.

Data with channel <- value written to a channel and read with variable = < channel, the variable assignment can be omitted when reading. The listening on a channel can also be automated with the for construct, the loop is exited when the channel is closed.

Example:

Package main   import " fmt "   func ten times (channel chan string) { / / Receive argument say: = < - channel   Back / / write ten times for i: = 0; i < 10; i { channel < - tell }   / / Close channel close (channel) }   func main () { / / Open synchronous channel channel: = make ( chan string) / / or make ( chan string, 0)   / / Start the parallel Go- routine " ten times ()" go ten times (channel)   / / Send a string channel <- "Hello"   / / Receive the string until the channel is closed for s: = range { channel fmt.Println ( s ) }   fmt.Println ("Done !") } In the example calls " main () ", the Go- routine " ten times () " which returns a string received ten times over the same channel and closes it afterwards. Due to the synchronous channel wait the two go- routines together, so main () occurs only in the for loop, if ten times () has received the string. Would not synchronous channel, a deadlock could occur if main () the variable being written is read immediately ( and removed from the buffer ) and ten times () waits in vain for his argument. It is also important that ten times () after writing the string to the channel closes, since main () otherwise the loop can not leave.

Implementations

There are at least two compilers for Go, which can be run on Linux, Mac OS X, Windows and FreeBSD and Go 1 specification fully implement:

Both compilers implement a parallel mark- and- sweep garbage collection.

The official compiler is accompanied by the command line tool go, which serves as a front for various tools, such as installing packages from source code repositories on the Internet such as GitHub or Google Code (go get), the automatic formatting source (go fmt ), the running tests (go test), the generating documentation from source code comments (go doc) or compile the project (go build), so that no makefiles are needed if a recommended directory structure is maintained.

History

The design phase began on 21 September 2007, at first. Than 20 percent project on its own initiative by Robert Griesemer, Rob Pike and Ken Thompson Soon after, other developers pushed to Go and became a full -time project. On 30 October 2009 Go was presented by Rob Pike at a Google TechTalk and announced the release of a free software that, as announced, was then carried out on November 10. Since then, numerous contributions from developers in the Go community have been added outside of Google. On March 28, 2012 Version 1 was released. Since language specification and standard library are considered to be stable and should not have to source - level backward - incompatible changes be subjected within the 1.x series. On May 14, 2013 Go 1.1 has been released, containing mainly performance enhancements in the implementation.

271082
de