GO PROGRAMMING LANGUAGE Technical seminar on P.VIvek REG NO. 0801209401 CSE-B
May 28, 2015
GO PROGRAMMING LANGUAGE
Technical seminar on
P.VIvekREG NO. 0801209401CSE-B
Go is a• New• Experimental• Concurrent• Garbage-Collected • Systems Language.
Why a new language?
• Go fast!• Make programming fun again.• 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.
Features
The most important features of Go are• the feel of a dynamic language with the safety
of a static type system.• compile to machine language so it runs fast.• real run-time that supports GC, concurrency.• lightweight, flexible type system.• has methods but not a conventional object-oriented language.
Compiled• Go is a compiled language. At the moment
there are two compilers.• Gccgo is a Go compiler that uses the GCC
back end. There is also a suite of compilers with different (and odd) names for each architecture: 6g for the 64-bit x86, 8g for the 32-bit x86, and more.
• These compilers run significantly faster but generate less efficient code than gccgo.
Similarity
• Go is having a similar language structure as of C.
• Declarations are introduced by a keyword (var,
const, type, func) and look reversed compared to C. Examples are
var i int const PI = 22/7
The “:=“short declaration
• Within functions (only), declarations of the form var v = value can be shortened to v := value (Another reason for the name/type reversal.)
• The type is that of the value (for ideal numbers, get int or float, accordingly.)
• a, b, c, d := 1, 2.0, "three", FOUR• These are used a lot and are
available in places such as for
loop initializers.
ZERO
• All memory in Go is initialized. All variables are initialized upon execution of their declaration.
• Without an initializing expression, the "zero value" of the type is used. • The loop for i := 0; i < 5; i++ {
var v int; fmt.Printf("%d\n", v);
v = 5 }
will print 0, 0, 0, 0, 0.• The zero value depends on the type:
integer 0, floating point 0.0,
false, empty string, nil pointer, zeroed struct, etc.
SCOPE• Within a package, all global variables, functions, types, and
constants are visible from all the package's source files.• For clients (importers) of the package, names must be
upper case to be visible: global variables, functions, types, constants, plus methods and structure fields for global variables and types.
const hello = "you smell" // package visibleconst Hello = "you smell nice" // globally visibleconst _Bye = "stinko!" // _ is not upper
• Very different from C/C++: no extern, static, private, public.
SLICE• A slice is a reference to a section of an array.• Slices are used much more often than plain
arrays. A slice is very cheap. • A slice type looks like an array type without a
size: var a [] int• Create a slice by "slicing" an array or slice: a = ar[7:9];• Valid indexes of a will be 0, 1
and len(a)==2.
MAPs
• Maps are another reference type. They are declared like this:
var m map[string] float• This declares a map indexed with key type
string and value type float. It is analogous to the C++
type *map<string,float> (note the *).• On a map, len() returns the number of keys.
INTERFACE• The word "interface" is a bit overloaded in Go:• There is the concept of an interface, and there is an interface type,
and then there are values of that type. First, the concept.Definition:
• An interface is a set of methods. An interface type is a specification of an interface, a set of methods implemented by some other types.
• Here's a simple one, with only one method:type AbsInterface interface {Abs() float // receiver is implied}
CONCURRENCY
• A goroutine is a Go function or method executing concurrently in the same address space as other goroutines.
• A running program consists of one or more goroutines.
• It's not the same as a thread, coroutine, process, etc. It's a goroutine.
GOROUTINES
• Goroutines are cheap.• Goroutines exit by returning from their
toplevel function, or just falling off the end. Or they can call runtime.Goexit(), although that's rarely necessary.
• Goroutines can run concurrently on different processors, sharing memory.
GOROUTINES
• Goroutines are multiplexed as needed onto system threads. When a goroutine executes a blocking system call, no other goroutine is blocked.
• We will do the same for CPU-bound goroutines at some point, but for now, if you want user-level parallelism you must set
$GOMAXPROCS. or call runtime.GOMAXPROCS(n).
• GOMAXPROCS tells the runtime scheduler how many non-syscall-blocked goroutines to run at once.
CHANNELS
• Unless two goroutines can communicate, they can't coordinate.
• Go has a type called a channel that provides communication and synchronization capabilities.
• It also has special control structures that build on channels to make concurrent programmingeasy.
Example Program
• package mainimport "fmt"func main() {
fmt.Print("Hello,\n")}
CONCLUSION
• It's early yet but promising.• A very comfortable and productive language.• Lots of documents on the web: specification,
tutorial, "Effective Go", FAQs, more.Full open-source implementations.
• Want to try it?Want to help?Want to build libraries or tools?
http://golang.org
Any Queries??