CSC/ECE 517 Fall 2014/ch1a 10 hu

From Expertiza_Wiki
Revision as of 22:32, 19 September 2014 by Vsonthy (talk | contribs) (→‎Go Compilers)
Jump to navigation Jump to search

Go, also commonly referred to as golang, is a compiled, concurrent, garbage-collected, statically typed language developed at Google[1]. It was developed by Robert Griesemer[2], Rob Pike[3] and Ken Thompson[4] in 2007. It was announced in November 2009 and is currently being used in many production systems at Google. It is an open source project. The syntax is loosely derived from that of C programming language, adding garbage collection, type safety, some dynamic-typing capabilities. Additional built-in types such as variable-length arrays and key-value maps, and a large standard library are also supported. Go's "gc" compiler targets the Linux, Mac OS X, FreeBSD, NetBSD, OpenBSD, Plan 9, and Microsoft Windows operating systems and the i386, amd64, and ARM processor architectures.

Background

Go programming started of as a research project by Robert Griesemer, Rob Pike and Ken Thompson. They felt that there was no new system focused programming language that has come up in the last decade till 2008, while there were other developments like increase in the size of libraries, dominance of networking , client server focus, multi-core CPUs. So they wanted to come up with a new systems focused programming language that would address the above issues. They started sketching the goals for a new language on the white board on September 21, 2007. Within a few days the goals had settled into a plan to do something and a fair idea of what it would be. Design continued part-time in parallel with unrelated work. By January 2008, Ken had started work on a compiler with which to explore ideas; it generated C code as its output. By mid-year the language had become a full-time project and had settled enough to attempt a production compiler. In May 2008, Ian Taylor independently started on a GCC front end for Go using the draft specification. Russ Cox joined in late 2008 and helped move the language and libraries from prototype to reality. Go became a public open source project on November 10, 2009. Many people from the community have contributed ideas, discussions, and code.

Go Compilers

There are two Go compiler implementations, gc [5] (the 6g program and friends) and gccgo [6]. Gc uses a different calling convention and linker. It can therefore only be linked with C programs using the same convention. There is such a C compiler but no C++ compiler. Gccgo is a GCC front-end that can, with care, be linked with GCC-compiled C or C++ programs.

The cgo program provides the mechanism for a “foreign function interface” to allow safe calling of C libraries from Go code. SWIG extends this capability to C++ libraries.

Runtime support for Go

Again due to bootstrapping issues, the run-time code is mostly in C (with a tiny bit of assembler) although Go is capable of implementing most of it now. Gccgo's run-time support uses glibc. Gc uses a custom library to keep the footprint under control; it is compiled with a version of the Plan 9 C compiler that supports resizable stacks for goroutines. The gccgo compiler implements these on Linux only, using a technique called segmented stacks, supported by recent modifications to the gold linker.


Language Design

Syntax

 package main //line 1

   import “fmt”  //line 2

   func main() {  //line 3

       fmt.Println(“Hello World”) //line 4

   } //line 5

Line1 specifies package declaration. Package is used to organize and reuse code. Line 2 is used for including the package “fmt” which is used for formatting input and output. Line 3 to 5 defines the function main. main is the function which is called when a program is executed. Println function in fmt is used to display content to the console i.e in this example Hello World is displayed in the console on executing the program.

Code Conventions and Style

  • Go fmt tool is used to automatically standardize indentation, spaces and surface-level code details. go vet and go link are perform checking automatically.
  • Using tools and libraries provided in Go, there are standard approaches for API documentation(go doc), testing(go test), building(go build) and package management(go get).

Types

  • Numbers are represented in Go as either Integers or Floating point numbers.
    • Integers: Used to represent numbers without a decimal component. Different integers supported by Go are: uint8, uint16, uint32, uint64, int8, int16, int32, int64. Uint is used for unsigned integers and int for signed. 8,16, 32, 64 indicate the number of bits each type would use.
    • Floating Point Integers: Used to support decimal components. Go supports float32 and float64 to represent floating point numbers and complex32 and complex64 to represent complex numbers.

Example:


  • String: String litersals can be created using either double quotes(“Hello”) or back ticks(`Hello`). Strings have several operations associated them, for example one can find the length of the string or get a character at a specific index in a string.

Example:

package main

import “fmt”

func main() {

	fmt.Println(len(“Hello”)) // output: 5
	fmt.Println(“Hello”[1]) // output: 1
	fmt.Println(“Hello “+”Tom”) //output: Hello Tom

}

  • Boolean: A 1 bit value is used to represent true or false.

Example:


Variables and Constants

  • Variabes in Go are indicated using the var keyword. Example: var x string=”hello” . They can also be shorthanded as x:=”hello” .
  • The variable names should begin with a letter and can contain letters, digits or underscore character.
  • The scope of the variable is the block in which it is defined and an attempt to access it outside the block would result in an error on running the program.
  • Go provides support for constants (i.e. variables whose value can not be changed), using the const keyword. Example: const temp string = “Hello”
  • In Go one can define multiple variables(or constants) using the following syntax:
   var(
        t1 =1
        t2=2
        t3=3
       t4=4

)

Control Structures

Arrays, Slices and Maps

Functions

Pointers

Concurrency

Applications

where it is currently being used Write stuff from the actual wiki page . It can be used to develop a server

Users of Go

Apart from Google, which uses Go programming for some of its development, companies like Dropbox, CloudFlare, SoundCloud, The BBC, Novartis, Cloud Foundry also use Go programming. Dropbox server (running in the cloud) and desktop client software were primarily written in Python. From mid-2013 Dropbox began migrating its backend infrastructure to Go. Some of the open-source applications that are using Go programming are Docker, Flynn, PaaS, Juju, nsq, Doozer e.t.c



Advantages

C Level Performance -- golang is within a close margin of the performance level of C code (which is itself an extremely high performance language). It is certainly closer to the performance of native languages than other enterprise level languages such as Java, Scala, Erlang etc. Illustration Reduced compile time is a key improvement in the Go language If A.go depends on B.go depends on C.go : · Compile C.go, B.go then A.go normally(other languages) · To compile A.go, compiler reads B.o not C.o · At a large scale this should help in hugely speeding up

· Fast Compilation -- The Golang compiler was designed to compile programs very quickly. Extremely large programs with massively high LOC counts compile seemingly instantly - afterall, Golang was designed by Google to solve Google problems, and Google has big problems! At Google, code and CPU cores are both measured in the millions. Go provides a model for software construction that makes dependency analysis easy and avoids much of the overhead of C-style include files and libraries. · Concurrency Supported At Language Level -- Concurrency is the ability to execute multiple computations simultaneuously. In today's modern multicore and high-load computing environment, concurrency is a critical language feature, and one in which languages such as Java struggle immensely. · By its design, Go proposes an approach for the construction of system software on multicore machines. · Google have summed things up nicely with their motto: "Don't communicate by sharing memory, share memory by communicating. "Go's type system has no hierarchy, so no time is spent defining the relationships between types. Also, although Go has static types the language attempts to make types feel lighter weight than in typical OO languages. · Garbage Collection -- In C and C++, programmers spend a lot of time and energy on allocating and freeing memory. The designers of Go wanted to remove that complication, and ensure Go programmers could concentrate on more productive tasks. As such, they implemented garbage collection. As a developer, this is a very good thing. Sure, garbage collection comes with its own set of issues, but those are dealt with by the language implementer while the programmer is free to reap the other many benefits of Go.

Limitations

· Go is still an experimental language subject to change. · Go's not very usable on Windows yet. · The packages distributed with Go are pretty useful, but there are still some libraries you'll miss. Most notably a UI toolkit. · There is no support for generics in Go, although there are many discussions around it.


Design Principles

Programming today involves too much bookkeeping, repetition, and clerical work. As Dick Gabriel says, “Old programs read like quiet conversations between a well-spoken research worker and a well-studied mechanical colleague, not as a debate with a compiler. Who'd have guessed sophistication bought such noise?” The sophistication is worthwhile—no one wants to go back to the old languages—but can it be more quietly achieved?

Go attempts to reduce the amount of typing in both senses of the word. Throughout its design, we have tried to reduce clutter and complexity. There are no forward declarations and no header files; everything is declared exactly once. Initialization is expressive, automatic, and easy to use. Syntax is clean and light on keywords. Stuttering (foo.Foo* myFoo = new(foo.Foo)) is reduced by simple type derivation using the := declare-and-initialize construct. And perhaps most radically, there is no type hierarchy: types just are, they don't have to announce their relationships. These simplifications allow Go to be expressive yet comprehensible without sacrificing, well, sophistication.

Another important principle is to keep the concepts orthogonal. Methods can be implemented for any type; structures represent data while interfaces represent abstraction; and so on. Orthogonality makes it easier to understand what happens when things combine.