developed ~2007 by Robert Griesemer, Rob Pike, Ken Thompson open source

Similar documents

Lecture 4: Golang Programming. Lecturer: Prof. Zichen Xu

Go Cheat Sheet. Operators. Go in a Nutshell. Declarations. Basic Syntax. Hello World. Functions. Comparison. Arithmetic. Credits

Python source materials

Introduzione a Go e RPC in Go

Lecture 5: Python PYTHON

Let s Go! Akim D le, Etienne Renault, Roland Levillain. June 8, TYLA Let s Go! June 8, / 58

Go Tutorial. To do. A brief, gentle intro to Go. Next Networking. q Today

Go for Java Developers

The Go Programming Language. Frank Roberts

Lecture Overview Methods and Interfaces Methods review Interfaces Example: using the sort interface Anonymous fields in structs

Go Tutorial. Arjun Roy CSE 223B, Spring 2017

Go Forth and Code. Jonathan Gertig. CSC 415: Programing Languages. Dr. Lyle

Motivations History Principles Language Gommunity Success stories Conclusion. Let s Go! A brief introduction to Google s new language.

Go Language September 2016

GO IDIOMATIC CONVENTIONS EXPLAINED IN COLOR

Go on NetBSD (and pkgsrc!) A modern systems programming language 23 March Benny Siegert Google Switzerland; The NetBSD Foundation

Summary of Go Syntax /

Jython. An introduction by Thinh Le

Go Concurrent Programming. QCon2018 Beijing

The Awesomeness of Go. Igor Lankin DevFest Karlsruhe, Nov 2016

understanding

Tranquility Publications. Web Edition MAC

1 Shyam sir JAVA Notes

New Parallel Programming Languages for Optimization Research

Lecture 8 (7.5?): Javascript

CS313D: ADVANCED PROGRAMMING LANGUAGE

M/s. Managing distributed workloads. Language Reference Manual. Miranda Li (mjl2206) Benjamin Hanser (bwh2124) Mengdi Lin (ml3567)

Over-simplified history of programming languages

Scripting languages. shell programs are good for personal tools

Introduction to Programming Using Java (98-388)

Distributed Systems. 02r. Go Programming. Paul Krzyzanowski. TA: Yuanzhen Gu. Rutgers University. Fall 2015

Don t Go Java! Edition 2019

Chapter 1 INTRODUCTION SYS-ED/ COMPUTER EDUCATION TECHNIQUES, INC.

Scripting languages. shell programs are good for personal tools

CS4120/4121/5120/5121 Spring 2016 Xi Language Specification Cornell University Version of May 11, 2016

CS 11 java track: lecture 1

Short Notes of CS201

Programming Language Basics

CS201 - Introduction to Programming Glossary By

GOLD Language Reference Manual

Go Programming. Russ Cox and Rob Pike May 20, 2010

Organization of Programming Languages CS320/520N. Lecture 06. Razvan C. Bunescu School of Electrical Engineering and Computer Science

The Design of C: A Rational Reconstruction: Part 2

Continued from previous lecture

Homework 5: Spatial Games : Programming for Scientists Due: Thursday, March 3, 2016 at 11:59 PM

Issue with Implementing PrimeSieve() in Go

Cut. it s not just for Google. Eleanor McHugh.

CS 251 Intermediate Programming Java Basics

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

egrapher Language Reference Manual

CPSC 3740 Programming Languages University of Lethbridge. Data Types

Over-simplified history of programming languages

Using the Go Programming Language in Practice

Introduction to the Go Programming Language

The Pyth Language. Administrivia

Homework 6: Spatial Games Due: 11:59pm on Friday, October 30

CSI33 Data Structures

Go vs. Swift: The Languages of The Modern Tech Giants

Slang Reference Manual

THE GO PROGRAMMING LANGUAGE CHARACTERISTICS AND CAPABILITIES

Compsci 101 Fall 2015 Exam 1 Rubric. Problem 1 (24 points)

GO - QUICK GUIDE GO - OVERVIEW

05-concurrency.txt Tue Sep 11 10:05: , Fall 2012, Class 05, Sept. 11, 2012 Randal E. Bryant

Index. object lifetimes, and ownership, use after change by an alias errors, use after drop errors, BTreeMap, 309

CMSC 330: Organization of Programming Languages. Rust Basics

Arrays and Strings

Type Conversion. and. Statements

Fall 2017 CISC124 9/16/2017

Pierce Ch. 3, 8, 11, 15. Type Systems

Your first C++ program

Flow Control. CSC215 Lecture

Java Basic Programming Constructs

LECTURE 18. Control Flow

.Net Technologies. Components of.net Framework

COMP520 - GoLite Type Checking Specification

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

ECEN 449 Microprocessor System Design. Review of C Programming

Appendix. Grammar. A.1 Introduction. A.2 Keywords. There is no worse danger for a teacher than to teach words instead of things.

ECEN 449 Microprocessor System Design. Review of C Programming. Texas A&M University

CSCI-GA Scripting Languages

Data Types, Variables and Arrays. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

Beyond Blocks: Python Session #1

Crash Course in Java. Why Java? Java notes for C++ programmers. Network Programming in Java is very different than in C/C++

Client-Side Web Technologies. JavaScript Part I

Another Go at Language Design

Introduce C# as Object Oriented programming language. Explain, tokens,

Flow Language Reference Manual

PIC 20A The Basics of Java

MPATE-GE 2618: C Programming for Music Technology. Syllabus

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Distributed Real-Time Control Systems. Lecture 17 C++ Programming Intro to C++ Objects and Classes

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Scientific GPU computing with Go A novel approach to highly reliable CUDA HPC 1 February 2014

COMP520 - GoLite Type Checking Specification

Chapter 2: Using Data

CS 11 Haskell track: lecture 1

Transcription:

Go developed ~2007 by Robert Griesemer, Rob Pike, Ken Thompson open source compiled, statically typed syntax looks sort of like C garbage collection built-in concurrency no classes or type inheritance or overloading or generics unusual interface mechanism instead of inheritance

Go source materials official web site: golang.org Go tutorial, playground Rob Pike on why it is the way it is: http://www.youtube.com/watch?v=rkndgt73v8s Russ Cox on interfaces, reflection, concurrency http://research.swtch.com/gotour

Hello world in Go package main! import "fmt"! func main() {! fmt.println("hello, World")! $ go run hello.go # to compile and run! $ go help # for more!

Go constructs constants, variables, types operators and expressions statements, control flow data: structs, pointers, arrays, slices, maps functions libraries and packages interfaces concurrency: goroutines, channels etc.

Constants, variables, operators constants bool, string; int, float, complex (all of various sizes) quotes: char, string! const pi = 3.14! const World = " " // strings are unicode! variables var x, y, z = 0, 1.23, false // global variables! x := 0; y := 1.23; z := false // inside a function! Go infers the type from the initializer assignment between items of different type requires an explicit conversion, e.g., int(float expression) operators mostly like C, but ++ and -- are postfix only and not expressions assignment is not an expression string concatenation uses +

Statements, control flow: if-else statements assignment, control flow, function call, scope indicated by mandatory braces; no ; terminator needed control flow: if-else, for, switch if opt-stmt; boolean {! statements! } else if opt-stmt; boolean {! statements! } else {! statements! if c := getchar(); c!= EOF { // scope of c is whole if-else!...!

More control flow: for Looping with for for opt-stmt; boolean; opt-stmt { // can drop stmts and ;'s! statements // break, continue (with optional labels) for {! // runs for a long time! for index := range something {!...!

More control flow: switch Switch switch opt-stmt; opt-expr {! case exprlist: statements case exprlist: statements! default: statements! // no fallthrough! switch Suffix(file) {! case ".gz": return GzipList(file)! case ".tar": return TarList(file)! case ".zip": return ZipList(file)! can also switch on types

Structs and pointers (adapted from Go Tour) type Vertex struct {! X, Y int! var (! p = Vertex{1, 2} // has type Vertex! )! q = &Vertex{1, 2} // has type *Vertex! r = Vertex{X: 1} // Y:0 is implicit! s = Vertex{} t = new(vertex) func main() {! // X:0 and Y:0! fmt.println(p, q, r, s, t)! // pointer to a 0,0 vertex!

Arrays and slices an array is a fixed-length sequence of same-type items a slice is a variable-length but fixed capacity food := []string {"beer", "pizza", "coffee"} use make to create new slices food := make([]string, 3, 10) // initial len, [capacity]! elements accessed as slice[index] indices from 0 to len(slice)-1 inclusive slicing: food[start:end] is elements start..end-1 slices are very efficient (passed as small structures) arrays are passed by value most library functions work on slices slices are mutable: if the slice changes, that's visible to all variables that refer to it

Maps (== associative arrays) unordered collection of key-value pairs keys are any type that supports == and!= operators (e.g., built-ins) values are any type m := map[string] int {"pizza":200, "beer":100} m["coke"] = 50 wine := m["wine"] // 0 if not there coffee, found := m["coffee"] // 0, false if not present delete(m, "chips") // ok if not present

Functions func name(arg, arg, arg) (ret, ret) { statements of function } func div(num, denom int) (q, r int) { // computes quotient & remainder. denom should be > 0 q = num / denom r = num % denom return // returns two named values, q and r } functions are objects can assign them, pass them to functions, return them from functions parameters are passed call by value (including arays!) functions can return any number of results defer statement queues operation until function returns defer f.close()!

Methods & pointers can define methods on any type, including your own: type Vertex struct {! X, Y float64! func (v *Vertex) Scale(f float64) {! v.x = v.x * f! v.y = v.y * f! func (v *Vertex) Abs() float64 {! return math.sqrt(v.x*v.x + v.y*v.y)! func main() {! v := &Vertex{3, 4 v.scale(5)! fmt.println(v, v.abs())!

Methods, pointers and interfaces can attach methods to any type fmt package uses %s to print anything that has a String() method %v uses reflection to print any type at all type information and some basic operations available at run time type World int // defines a new type. could be any type here! func (w *World) String() string { // receiver w unused here! return "world"! func main() {! fmt.println("hello, ")! fmt.println("hello,", new(world))!

Interfaces type Writer interface {! Write(p []byte) (n int, err error)! an interface is satisfied by any type that implements all the methods of the interface completely abstract: can't instantiate one can have a variable with an interface type then assign to it a value of any type that has the methods the interface requires interface{} is empty set of methods so every value satisfies interface{ a type implements an interface merely by defining the required methods it doesn't declare that it implements them

Sort interface Sort interface defines three methods any type that implements those three methods can sort // Package sort provides primitives for sorting slices! // and user-defined collections.! package sort! // A type, typically a collection, that satisfies sort.interface! // can be sorted by the routines in this package. The methods! // require that the elements of the collection be enumerated by! // an integer index.! type Interface interface {! // Len is the number of elements in the collection.! Len() int! // Less reports whether the element with! // index i should sort before the element with index j.! Less(i, j int) bool! // Swap swaps the elements with indexes i and j.! Swap(i, j int)!

Sort interface (adapted from Go Tour) type Person struct {!!Name string!!age int! func (p Person) String() string {!!return fmt.sprintf("%s: %d", p.name, p.age)! type ByAge []Person! func (a ByAge) Len() int { return len(a) func (a ByAge) Swap(i, j int) { a[i], a[j] = a[j], a[i] func (a ByAge) Less(i, j int) bool { return a[i].age < a[j].age func main() {!!people := []Person{{"Bob",31}, {"Sue",42}, {"Ed",17}, {"Jen",26},!fmt.Println(people)!!sort.Sort(ByAge(people))!!fmt.Println(people)!

Concurrency: goroutines & channels channel: a type-safe generalization of Unix pipes inspired by Hoare's Communicating Sequential Processes goroutine: a function executing concurrently with other goroutines in the same address space run multiple parallel computations simultaneously loosely like threads but very much lighter weight channels coordinate computations by explicit communication no locks, semaphores, mutexes, etc

Example: web crawler (with thanks to Russ Cox's video) want to crawl a bunch of web pages to do something e.g., figure out how big they are problem: network communication takes relatively long time program does nothing useful while waiting for a response solution: access pages in parallel send requests asynchronously display results as they arrive needs some kind of threading or other parallel process mechanism takes less time than doing them sequentially

Declarations package main! import "fmt"! import "io"! import "io/ioutil"! import "net/http"! import "time"! type Site struct {! Name string! URL string! var sites = []Site {! {"Go", "http://golang.org"},! {"Python", "http://python.org"},! {"Scala", "http://scala-lang.org"},! {"Ruby", "http://ruby-lang.org"},! {"Perl", "http://perl.org"},!

Version 1: no parallelism func main() {! start := time.now()! for _, site := range sites {! count(site.name, site.url)! fmt.printf("%.2fs total\n", time.since(start).seconds())! func count(name, url string) {! start := time.now()! r, err := http.get(url)! if err!= nil {! fmt.printf("%s: %s\n", name, err)! return! n, _ := io.copy(ioutil.discard, r.body)! r.body.close()! dt := time.since(start).seconds()! fmt.printf("%s %d [%.2fs]\n", name, n, dt)!

Version 2: parallelism with goroutines func main() {! start := time.now()! c := make(chan string)! n := 0! for _, site := range sites {! n++! go count(site.name, site.url, c)! for i := 0; i < n; i++ {! fmt.print(<-c)! fmt.printf("%.2fs total\n", time.since(start).seconds())! func count(name, url string, c chan<- string) {! start := time.now()! r, err := http.get(url)! if err!= nil {! c <- fmt.sprintf("%s: %s\n", name, err)! return! n, _ := io.copy(ioutil.discard, r.body)! r.body.close()! dt := time.since(start).seconds()! c <- fmt.sprintf("%s %d [%.2fs]\n", name, n, dt)!

Version 2: main() for parallelism with goroutines func main() {! start := time.now()! c := make(chan string)! n := 0! for _, site := range sites {! n++! go count(site.name, site.url, c)! for i := 0; i < n; i++ {! fmt.print(<-c)! fmt.printf("%.2fs total\n", time.since(start).seconds())!

Version 2: count() for parallelism with goroutines func count(name, url string, c chan<- string) {! start := time.now()! r, err := http.get(url)! if err!= nil {! c <- fmt.sprintf("%s: %s\n", name, err)! return! n, _ := io.copy(ioutil.discard, r.body)! r.body.close()! dt := time.since(start).seconds()! c <- fmt.sprintf("%s %d [%.2fs]\n", name, n, dt)!

Review: Formatter in AWK /./ { for (i = 1; i <= NF; i++) addword($i) } /^$/ { printline(); print "" } END { printline() } function addword(w) { if (length(line) + length(w) > 60) printline() line = line space w space = " " } function printline() { if (length(line) > 0) print line line = space = "" }

Formatter in Go var line, space = "", "" func main() { scanner := bufio.newscanner(os.stdin) for scanner.scan() { if line := scanner.text(); len(line) == 0 { printline() fmt.println() } else { for _, wds := range strings.fields(line) { addword(wds) } } } printline() } func addword(word string) { if len(line) + len(word) > 60 { printline() } line = line + space + word space = " " } func printline() { if len(line) > 0 { fmt.println(line) } line = ""; space = "" }