Go Tutorial. Ian Lance Taylor. Introduction. Why? Language. Go Tutorial. Ian Lance Taylor. GCC Summit, October 27, 2010

Similar documents
Introduction to the Go Programming Language

Google Go illustrated on the basis of Fibonacci numbers

Do we have a quorum?

Trace semantics: towards a unification of parallel paradigms Stephen Brookes. Department of Computer Science Carnegie Mellon University

Operating Systems. VII. Synchronization

Administrivia. Course Objectives. Overview. Lecture Notes Week markem/cs333/ 2. Staff. 3. Prerequisites. 4. Grading. 1. Theory and application

Clojure Concurrency Constructs, Part Two. CSCI 5828: Foundations of Software Engineering Lecture 13 10/07/2014

INF Models of concurrency

Safety and Liveness. Thread Synchronization: Too Much Milk. Critical Sections. A Really Cool Theorem

CS 347 Parallel and Distributed Data Processing

Blocking Synchronization: Streams Vijay Saraswat (Dec 10, 2012)

Compiling Techniques

Outline F eria AADL behavior 1/ 78

High Performance Computing

Energy-Efficient Broadcast Scheduling. Speed-Controlled Transmission Channels

Introduction to Axiomatic Semantics

Antonio Falabella. 3 rd nternational Summer School on INtelligent Signal Processing for FrontIEr Research and Industry, September 2015, Hamburg

Socket Programming. Daniel Zappala. CS 360 Internet Programming Brigham Young University

Maps performance tips On server: Maintain DB connections, prepared statements (per thread/request!)

Timeline of a Vulnerability

Declarative Computation Model. Conditional. Case statement. Procedure values (2) Procedure values. Sequential declarative computation model

Formal Conformance Testing 2006

Modeling Concurrent Systems

Timeline of a Vulnerability

RSA Key Extraction via Low- Bandwidth Acoustic Cryptanalysis. Daniel Genkin, Adi Shamir, Eran Tromer

PySy: A Python Package for Enhanced Concurrent Programming. TODD WILLIAMSON B.S (University of California at Davis) 2007 THESIS

Real Time Operating Systems

ASYMPTOTIC COMPLEXITY SEARCHING/SORTING

A Short Introduction to Hoare Logic

CIS 4930/6930: Principles of Cyber-Physical Systems

Design Patterns and Refactoring

Verification of a Dynamic Channel Model using the SPIN Model Checker

Notes 11: OLS Theorems ECO 231W - Undergraduate Econometrics

Consensus and Universal Construction"

import java. u t i l. ;... Scanner sc = new Scanner ( System. in ) ;

GPU Acceleration of Cutoff Pair Potentials for Molecular Modeling Applications

Satellite project, AST 1100

Sharing Objects. Pieter van den Hombergh. Fontys Hogeschool voor Techniek en Logistiek. February 15, 2017

Time, Clocks, and the Ordering of Events in a Distributed System

Techniques of Java Programming

Formal Methods in Software Engineering

Coordination. Failures and Consensus. Consensus. Consensus. Overview. Properties for Correct Consensus. Variant I: Consensus (C) P 1. v 1.

ITI Introduction to Computing II

Formal Verification Techniques. Riccardo Sisto, Politecnico di Torino

1 Trees. Listing 1: Node with two child reference. public class ptwochildnode { protected Object data ; protected ptwochildnode l e f t, r i g h t ;

Communicating Sequential Processes in High Performance Computing

Coding for loss tolerant systems

The State Explosion Problem

DiffSharp An AD Library for.net Languages

Adapted with permission from: Seif Haridi KTH Peter Van Roy UCL. C. Varela; Adapted w. permission from S. Haridi and P. Van Roy 1

Chandy-Lamport Snapshotting

CHAPTER 5 - PROCESS SCHEDULING

Applied C Fri

DISTRIBUTED COMPUTER SYSTEMS

Time. To do. q Physical clocks q Logical clocks

Graphical User Interfaces for Emittance and Correlation Plot. Henrik Loos

10:00 12:30. Do not open this problem booklet until the start of the examination is announced.

compare to comparison and pointer based sorting, binary trees

Causal Broadcast Seif Haridi

Science Analysis Tools Design

Distributed Systems Principles and Paradigms. Chapter 06: Synchronization

Distributed Systems Principles and Paradigms

ITI Introduction to Computing II

Towards a Practical Secure Concurrent Language

Che-Wei Chang Department of Computer Science and Information Engineering, Chang Gung University

Prioritized Garbage Collection Using the Garbage Collector to Support Caching

Clock Synchronization

Distributed Computing in Shared Memory and Networks

Experiments with Measuring Time in PRISM 4.0 (Addendum)

Recap. CS514: Intermediate Course in Operating Systems. What time is it? This week. Reminder: Lamport s approach. But what does time mean?

Course Announcements. Bacon is due next Monday. Next lab is about drawing UIs. Today s lecture will help thinking about your DB interface.

Project # Embedded System Engineering

Design of Real-Time Software

Unit: Blocking Synchronization Clocks, v0.3 Vijay Saraswat

CS162 Operating Systems and Systems Programming Lecture 7 Semaphores, Conditional Variables, Deadlocks"

Process Scheduling for RTS. RTS Scheduling Approach. Cyclic Executive Approach

Complex Systems Design & Distributed Calculus and Coordination

Clocks in Asynchronous Systems

Chapter 11 Time and Global States

ECEN 651: Microprogrammed Control of Digital Systems Department of Electrical and Computer Engineering Texas A&M University

Extensibility Patterns: Extension Access

Checkpoint Questions Due Monday, October 1 at 2:15 PM Remaining Questions Due Friday, October 5 at 2:15 PM

Algorithms and Their Complexity

Adapted with permission from: Seif Haridi KTH Peter Van Roy UCL. C. Varela; Adapted w. permission from S. Haridi and P. Van Roy 1

Agreement. Today. l Coordination and agreement in group communication. l Consensus

An analogy from Calculus: limits

Our Problem. Model. Clock Synchronization. Global Predicate Detection and Event Ordering

Limitations of OCAML records

Biostatistics and Design of Experiments Prof. Mukesh Doble Department of Biotechnology Indian Institute of Technology, Madras. Lecture 11 t- Tests

Manual for some LCLS Matlab GUIs. Henrik Loos SLAC, 2575 Sand Hill Road, Menlo Park, CA 94025

Anatomy of SINGULAR talk at p. 1

Analysis of Algorithms

Queuing Networks. - Outline of queuing networks. - Mean Value Analisys (MVA) for open and closed queuing networks

UC Santa Barbara. Operating Systems. Christopher Kruegel Department of Computer Science UC Santa Barbara

Computer Science Introductory Course MSc - Introduction to Java

Actors for Reactive Programming. Actors Origins

Modern Functional Programming and Actors With Scala and Akka

An Automotive Case Study ERTSS 2016

Practical Data Processing With Haskell

Scheduling I. Today Introduction to scheduling Classical algorithms. Next Time Advanced topics on scheduling

Transcription:

GCC Summit, October 27, 2010

Go Go is a new experimental general purpose programming language. Main developers are: Russ Cox Robert Griesemer Rob Pike Ken Thompson It was released as free software in November 2009. http://golang.org/ This talk is about the language.

Talk I m not going to cover the basics of the language. The language is not large and you can all learn it easily. 1. Why introduce a new language? 2. Discuss some of the more unusual aspects of the language.

What problems does Go solve? Types get in the way too much. Multi-core is an opportunity. Concurrency is very hard to get right. Computers are fast but building programs is slow. Programming is less fun.

What problems does Go solve? Types get in the way too much. Multi-core is an opportunity. Concurrency is very hard to get right. Computers are fast but building programs is slow. Programming is less fun. Go is an attempt to solve these problems.

What problems does Go solve? Types get in the way too much. Go has a lightweight typing system, and many types need not be written explicitly. Multi-core is an opportunity. Concurrency is very hard to get right. Computers are fast but building programs is slow. Programming is less fun. Go is an attempt to solve these problems.

What problems does Go solve? Types get in the way too much. Go has a lightweight typing system, and many types need not be written explicitly. Multi-core is an opportunity. Go provides concurrent threads of execution as part of the language. Concurrency is very hard to get right. Computers are fast but building programs is slow. Programming is less fun. Go is an attempt to solve these problems.

What problems does Go solve? Types get in the way too much. Go has a lightweight typing system, and many types need not be written explicitly. Multi-core is an opportunity. Go provides concurrent threads of execution as part of the language. Concurrency is very hard to get right. Go provides channels for reliable communication: a CSP-like model. Computers are fast but building programs is slow. Programming is less fun. Go is an attempt to solve these problems.

What problems does Go solve? Types get in the way too much. Go has a lightweight typing system, and many types need not be written explicitly. Multi-core is an opportunity. Go provides concurrent threads of execution as part of the language. Concurrency is very hard to get right. Go provides channels for reliable communication: a CSP-like model. Computers are fast but building programs is slow. Go s package system minimizes the effect of dependencies. Programming is less fun. Go is an attempt to solve these problems.

What problems does Go solve? Types get in the way too much. Go has a lightweight typing system, and many types need not be written explicitly. Multi-core is an opportunity. Go provides concurrent threads of execution as part of the language. Concurrency is very hard to get right. Go provides channels for reliable communication: a CSP-like model. Computers are fast but building programs is slow. Go s package system minimizes the effect of dependencies. Programming is less fun. The language is small and does not get in your way. Go is an attempt to solve these problems.

Why a new language? The problems are inherent to existing languages. New libraries means moving in the wrong direction. Adding to something complex can only make it more complex. Go aims for simplicity. Go is an attempt to start over. Nothing has been added to Go unless there was a clear advantage for it discovered while writing real programs.

Performance The performance of the best Go code is unlikely to match the performance of the best C++ code. Go is safe, with array bounds checking and no pointer arithmetic. Go is garbage collected. Go does not have templates and thus does not have inline template specializations. Some form of generics are a possible future language extension. Gccgo does beat gcc on a few single-threaded benchmarks, and is generally competitive on most. Gccgo is significantly faster on some benchmarks where Go makes it easy to parallelize the problem.

Hello, Go package main import "fmt" func main() { fmt.print("hello, 世界 \n")

Slices // A s l i c e type has no l e n g t h. type A r r a y O f I n t [ 1 0 ] i n t type S l i c e O f I n t [ ] i n t // A s l i c e e x p r e s s i o n makes a s l i c e. var v = A r r a y O f I n t {1, 2, 3, 4, 5, 6, 7, 8, 9, 10 var s = v [ 2 : 5 ] // I n d e x l i k e an a r r a y. var x = s [ 0 ] // x == 3 // S l i c e s have a l e n g t h and a c a p a c i t y. // l e n ( s ) == 5 ; cap ( s ) == 8 // S l i c e i n d e x r a n g e s from 0 to l e n ( s ). // R e s l i c e a s l i c e to make i t l a r g e r. var s2 = s [ 0 : 8 ]

Methods Any type can have methods. // Uppercase names a r e p u b l i c. type Point s t r u c t { X, Y f l o a t func ( p Point ) Abs ( ) f l o a t { return math. Sqrt ( p. X p. X + p. Y p. Y) type P o l a r s t r u c t { R, Theta f l o a t func ( p P o l a r ) Abs ( ) f l o a t { return p. R

Interfaces 1 An interface type is a list of methods. An interface value may hold any value whose type implements those methods. type Abser i n t e r f a c e { Abs ( ) f l o a t func f ( ) { p o i n t := &Point {1, 2 var a Abser = p o i n t // S t a t i c type o f a i s ( always ) Abser. // Dynamic type o f a i s ( now ) Point. fmt. P r i n t ( a. Abs ( ) ) p o l a r := P o l a r {3, 4 a = p o l a r // Dynamic type o f a i s P o l a r. fmt. P r i n t ( a. Abs ( ) ) No need to explicitly declare that Point satisfies Abser.

Interfaces 2 The io package defines the io.writer interface. type W r i t e r i n t e r f a c e { Write ( p [ ] byte ) ( n i n t, e r r os. E r r o r ) Any type with a Write method with that signature satisfies the io.writer interface. Any function that needs to write something can take a io.writer as a parameter. E.g., fmt.fprintf takes an io.writer parameter. E.g., bufio.newwriter takes an io.writer and returns a buffered type that satisfies io.writer.

Interfaces 3 A value of one interface type may be converted to another interface type. The conversion succeeds if the dynamic type the type of the value stored in the interface supports all the methods of the destination interface. The conversion may fail at runtime with a panic. To dynamically test without panicking, use the comma ok form. a, ok := Abser ( p ) i f ok { // a i s not n i l. e l s e { // a i s n i l.

Goroutines Goroutines are concurrent execution threads. In gccgo there is one goroutine per pthread; in gc goroutines are multiplexed onto operating system threads. The go statement starts a goroutine. func f ( ) { go e x p e n s i v e C o m p u t a t i o n ( ) a n o t h e r E x p e n s i v e C o m p u t a t i o n ( )

Channels 1 To communicate with a goroutine, use a channel. func computeandsend ( ch chan i n t ) { ch < e x p e n s i v e C o m p u t a t i o n ( ) func f ( ) { ch := make ( chan i n t ) go computeandsend ( ch ) v2 := a n o t h e r E x p e n s i v e C o m p u t a t i o n ( ) v1 := < ch fmt. P r i n t l n ( v1, v2 )

Channels 2 Channels are both a communication mechanism and a synchronization mechanism. A channel write is a release operation on all memory stores before the write. A channel read is an acquire operation for all memory reads following the read. Go slogan: Do not communicate by sharing memory; instead, share memory by communicating.

A multiplexed server type Request s t r u c t { a, b i n t // r e p l y channel r e p l y c chan i n t type binop func (a, b int ) int func run ( op binop, req Request ) { req. replyc < op ( req. a, req. b) func s e r v e r ( op binop, s e r v i c e chan Request ) { f o r { req := < service // requests arrive here go run (op, req ) // don t wait f o r op func StartServer ( op binop ) chan Request { reqchan := make( chan Request ) go s e r v e r (op, reqchan ) return reqchan

The client // S t a r t s e r v e r ; r e c e i v e a c h a n n e l on which // to send r e q u e s t s. s e r v e r := S t a r t S e r v e r ( func ( a, b i n t ) i n t { return a+b ) // C r e a t e r e q u e s t s req1 := &Request {23,45, make ( chan i n t ) req2 := &Request { 17,1<<4, make ( chan i n t ) // Send them i n a r b i t r a r y o r d e r s e r v e r < req s e r v e r < req2 // Wait f o r the answers i n a r b i t r a r y o r d e r fmt. P r i n t f ( Answer2 : %d\n, < req2. r e p l y c ) fmt. P r i n t f ( Answer1 : %d\n, < req1. r e p l y c )

Defer The defer statement executes a call when the function returns. Deferred calls are executed in LIFO order. The defer statement implements a finally clause, but it is dynamic rather than syntactic. func p r i n t I n t ( i i n t ) { f o r i!= 0 { defer fmt. P r i n t ( i % 10) i /= 10

Panic and recover The panic function throws an exception. Runtime errors such as interface conversion failures call panic with a value that satisfies the runtime.error interface. A panic unwinds the stack, executing deferred functions. The recover function stops the stack unwind and returns the value passed to panic. If recover is called when no panic is in progress, recover returns nil. If a panic reaches the top of a goroutine stack without being recovered, it aborts the program. The recover function implements a catch clause, but it is dynamic rather than syntactic.

Panic and recover example func S a f e D i v i d e ( x, y i n t ) ( r e t i n t, e r r o r s t r i n g ) { defer func ( ) { i f e := r e c o v e r ( ) ; e!= n i l { i f y == 0 { e r r o r = d i v i s i o n by z e r o e l s e i f y == 1 { e r r o r = d i v i s i o n o v e r f l o w e l s e { p a n i c ( e ) ( ) r e t = x / y