23
Google’s Go Introduction to the next “C” language! S G Ganesh

Google's Go Programming Language - Introduction

Embed Size (px)

Citation preview

Page 1: Google's Go Programming Language - Introduction

Google’s GoIntroduction to the next “C” language!

S G Ganesh

Page 2: Google's Go Programming Language - Introduction

Agenda Introduction to Go (What is “Go”?) “Hello world” in Go Examples for “getting a hang of” Go Examples using some novel features Implementation status Resources Let’s Go!

Page 3: Google's Go Programming Language - Introduction

First things first: Setting the stage It’s not possible to cover all details of a new language in a

45-mins presentation We’ll quickly cover most important aspects of Go Can’t drink ocean in a day – let alone in an hour!

No one is an “expert” in Go yet Creators of the language are the only experts right now I am an early adopter and got hooked-on to the

language I am making this presentation based on my experience

For many questions, there are no right or wrong answers: Go is still an evolving language and answers might

change!

Page 4: Google's Go Programming Language - Introduction

So, what’s all the buzz about? Go: new programming language announced by Google (Sep 09)

Created lots of excitement in the programming community

Many tout it as the next C language ‘C’ evolved from ‘B’; many languages are named as ‘D’, or

want to be the ‘D’ language But nothing has made the cut so far; “Go” might (or will it be

“Gone” in a few years ;-) )

Is there substance behind hype? Yes, a lot! Most system programmers find it very good Go won Tiobe’s ‘language of the year award 2009’

Tiobe is a programming language popularity index: http://www.tiobe.com/

Latest status (march 2010)

Page 5: Google's Go Programming Language - Introduction

Okay, so what’s Go? Go is a new, experimental, concurrent, garbage-collected,

systems-programming language. New & Experimental: Go is still at experimental stage

with tools, packages etc. still in development. No production system implemented using Go till now

Concurrent: Supports 'communication channels’ for concurrency - Communicating Sequential Processes (CSP).

Garbage-collected: The memory is automatically garbage collected

For systems-programming: Intended for writing things like compilers, web servers…

Still, we can use it as a general purpose language.

Page 6: Google's Go Programming Language - Introduction

Who’s behind Go? Robert Griesemer, Ken Thompson (of Unix

fame), and Rob Pike are the creators of the language. All three are well-known in programming community

This is how things fell in place: Go project was started around May 2007. Ken Thompson

wrote a Go compiler and runtime from scratch. By mid of 2008, working compiler and runtime was ready. Ian Lance Taylor and Russ Cox joined Go team in 2008. Ian

Taylor implemented GCC front-end for Go.

Page 7: Google's Go Programming Language - Introduction

Why a new language? This description is as given by the creators

No major sys. programming language came-up in last decade. But much has changed during the last decade(s)

Libraries becoming bigger with lots of dependencies Internet and networking is becoming pervasive Client/server systems, massive clusters used today Multi-core processors becoming mainstream.

Systems programming languages were not designed with these in mind.

Other reasons construction (enterprise software) has become very slow OOP using inheritance hierarchies not effective

Page 8: Google's Go Programming Language - Introduction

Goals of the language Efficiency and ease of use:

Efficiency of C, but ease like Ruby. Performance: within 10%-20% of equivalent C

Safe to use: Both type-safe as well as memory-safe.

Concurrency: Good support for concurrency and communication

Garbage Collected: "Attempts" to build an efficient, and latency-free Garbage

Collection mechanism. High-speed builds:

Fast compilation & linking

Page 9: Google's Go Programming Language - Introduction

Some important capabilities of Go Simplicity: GO has a clean and concise syntax

Characteristic of Google products For example, light-weight type system

Use it to believe it

Separation of interface and the implementation I know it’s often misused statement, but Go has it! Arguably a novel feature of Go

Goroutines Is based on CSP: much safer than lock-based, like Java

And more: E.g. Reflection (yes! but this is systems prog. lang!)

Page 10: Google's Go Programming Language - Introduction

Enough theory, lets see examples!

All programs in Go should be in a package, its “main” here

We import “fmt” package for using Printf function Execution starts with ‘main.main()’ function Functions have “func” keyword Printf is in fmt package

Page 11: Google's Go Programming Language - Introduction

Now we’ll find factorial of a number

Lack of declarations “fact” and “i” inferred as “ints” from init value 1 because of :=

“for” loop is the only loop construct supported in Go Others like “while” are variations of “for” An example of minimal features

Note the lack of semi-colons Have to use only if “necessary”

Page 12: Google's Go Programming Language - Introduction

Looks like C, but not C!

Go has elegant declaration syntax See how arguments are passed and returned Not like C: it is infamous for its declaration syntax

Can return multiple values from functions See swap for similar functionality in = operator Example for “orthogonal” language features

Page 13: Google's Go Programming Language - Introduction

Built-in support for features like maps

Maps are built-in, so no need to import Initialized with pair separated by “:”

“range” keyword is useful for traversal Using a for loop Works for slices, strings etc. (“orthogonal” feature)

Page 14: Google's Go Programming Language - Introduction

Functions as first class objects

Functions are first class objects in Go We can have

“function literals” (similar to “closures” in functional languages) for example

Page 15: Google's Go Programming Language - Introduction

Structures

Structs are declared with type keyword We can have struct literals

Created in heap And print struct members using %v in Printf

Page 16: Google's Go Programming Language - Introduction

Methods

Methods are implemented by specifying the struct name before the method name

Page 17: Google's Go Programming Language - Introduction

Interfaces: A novel feature Interfaces specified with

‘interface’ keyword Not same as in C#/Java

The structs doesn’t have to say it implements an interface Any struct that implements

the methods as specified by any interface satisfies that interface

Strict static type checking & “duck typing”!

Page 18: Google's Go Programming Language - Introduction

Goroutines: easy & safe multithreading

Goroutines are functions executing in parallel in the same address space in stack

They communicate using “channels” (based on CSP) Cleaner, simpler and less-bug prone than using locks

This shows an example* of how a Sort on big list can be done in parallel with some other computation

Page 19: Google's Go Programming Language - Introduction

We haven’t covered a lot! Important features not covered because of limited time

reflection, embedding structs (aka inheritance), package construction etc.

Lots of libraries already implemented math, crypto, networking, regex, OS, testing, html gen….

Garbage collection & Go runtime capabilities Currently mark-and-sweep collector, but better ones under

construction Small runtime: GC, channels, stack allocation, goroutines

etc.

Page 20: Google's Go Programming Language - Introduction

Implementation status Currently compiler tool-chain available for Mac & Linux

No “official” windows port; “unofficial” old ports exist Compilers: GCC implementation and a stand-alone implementation You can download the compilers/tools from this website & try it It’s open source (BSD license): We can contribute!

Lots of work going on in libraries and improving the existing tool chain Frequent builds & releases, quick response times etc.

Many features & tools still lacking in Go For example, generics and debugger tool

Page 21: Google's Go Programming Language - Introduction

Resources Go websites:

Official: www.golang.org (web server implemented in Go!) Unofficial: http://go-lang.cat-v.org/

Want to learn Go? No “books” yet Read "Effective Go” (http://golang.org/doc/effective_go.html)

tutorials available online Tech talk (http://www.youtube.com/watch?v=rKnDgT73v8s) Join go-nuts mailing list [email protected]

You can try (compile & run) Go programs online! This is really useful: http://ideone.com/

Page 22: Google's Go Programming Language - Introduction

Q & A time

Page 23: Google's Go Programming Language - Introduction

So what are you waiting for?

Lets Go!