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

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

Introduzione a Go e RPC in Go

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

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

Go for Java Developers

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

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

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

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

GO IDIOMATIC CONVENTIONS EXPLAINED IN COLOR

The Go Programming Language. Frank Roberts

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

Go Language September 2016

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

Go Tutorial. Arjun Roy CSE 223B, Spring 2017

Tranquility Publications. Web Edition MAC

Jython. An introduction by Thinh Le

Go Concurrent Programming. QCon2018 Beijing

understanding

Course May 18, Advanced Computational Physics. Course Hartmut Ruhl, LMU, Munich. People involved. SP in Python: 3 basic points

Introduction to the Go Programming Language

Arrays and Strings

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

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

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

GO - QUICK GUIDE GO - OVERVIEW

New Parallel Programming Languages for Optimization Research

Using the Go Programming Language in Practice

Rheinisch-Westfälische Technische Hochschule Aachen. Lehrstuhl für Datenmanagement und -exploration Prof. Dr. T. Seidl. Proseminar.

Summary of Go Syntax /

go get my/vulnerabilities Green threads are not eco friendly threads

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

Erlang and Go (CS262a, Berkeley Fall 2016) Philipp Moritz

The Go Programming Language. Part 2

Slang Reference Manual

COMP520 - GoLite Type Checking Specification

COMP-520 GoLite Tutorial

Your first C++ program

THE GO PROGRAMMING LANGUAGE CHARACTERISTICS AND CAPABILITIES

Don t Go Java! Edition 2019

COMP322 - Introduction to C++ Lecture 01 - Introduction

COMP520 - GoLite Type Checking Specification

Advanced Programming Techniques. Networks. Christopher Moretti

Programming Language Basics

COMP520 - GoLite Type Checking Specification

The Pyth Language. Administrivia

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

Another Go at Language Design

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

Funk Programming Language Reference Manual

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

Remote Procedure Call

Java Application Development

Short Notes of CS201

HTTP. In this chapter we give an overview of HTTP, followed by the Go APIs to manage HTTP connections.

Remote Invocation. To do. Request-reply, RPC, RMI. q Today q. q Next time: Indirect communication

CS201 - Introduction to Programming Glossary By

Pace University. Fundamental Concepts of CS121 1

GOLD Language Reference Manual

Programming in C UVic SEng 265

Programming in C. What is C?... What is C?

Programming in C. What is C?... What is C?

Large-Scale Networks

APT Session 2: Python

Programming refresher and intro to C programming

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Lectures 5-6: Introduction to C

CS558 Programming Languages

Develop your Embedded Applications Faster: Comparing C and Golang. Marcin Pasinski Mender.io

CS558 Programming Languages

Beyond Blocks: Python Session #1

GO MOCK TEST GO MOCK TEST I

Syntax and Variables

Systems Programming. 05. Structures & Trees. Alexander Holupirek

func findbestfold(seq string, energyfunction func(fold)float64)) Fold {

8. The C++ language, 1. Programming and Algorithms II Degree in Bioinformatics Fall 2017

Types, Operators and Expressions

More Examples /

Python source materials

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

CS 11 java track: lecture 1

Accelerating Information Technology Innovation

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

Introduction to Bioinformatics

STSCI Python Introduction. Class URL

Functions & Variables !

Types, Operators and Expressions

D7020E. Robust and Energy-Efficient Real-Time Systems D7020E. Lecture 2: Components & the Timber language

D7020E. The dynamic evolution of a system Robust and Energy-Efficient Real-Time Systems. blocked Lecture 2: Components & the Timber language.

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

SPARK-PL: Introduction

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

Don t let data Go astray

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

egrapher Language Reference Manual

Twister: Language Reference Manual

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

COMP284 Scripting Languages Lecture 11: PHP (Part 3) Handouts

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

Transcription:

Lecture 22 Go

http://xkcd.com/979/

Go developed ~2007 at Google by Robert Griesemer, Rob Pike, Ken Thompson open sourced in 2009 compiled, statically typed very fast compilation C-like syntax garbage collection built-in concurrency no classes or type inheritance or overloading or generics unusual interface mechanism instead of inheritance

Influences

Outline history basic constructs, simple programs arrays & slices maps methods, interfaces concurrency, goroutines

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

Types, constants, variables basic types bool string int8 int16 int32 int64 uint8 int uint float32 float64 complex64 complex128 quotes:, UTF-8 string, `raw string` variables and declarations var c1, c2 rune var x, y, z = 0, 1.23, false // variable decls x := 0; y := 1.23; z := false // short variable decl Go infers the type from the type of 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 no?: operator

Echo command: // Echo prints its command-line arguments. package main import ( "fmt" ) "os" func main() { var s, sep string for i := 1; i < len(os.args); i++ { s += sep + os.args[i] sep = " " fmt.println(s)

Echo command (version 2): // Echo prints its command-line arguments. package main import ( "fmt" ) "os" func main() { s, sep := "", "" for _, arg := range os.args[1:] { s += sep + arg sep = " " fmt.println(s)

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

Control flow: 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) switch { case Suffix(file) == ".gz":... can also switch on types

Control flow: for for opt-stmt; boolean; opt-stmt { statements // break, continue (with optional labels) for boolean { // while statement for { // infinite loop for index, value := range something { //...

Arrays and slices an array is a fixed-length sequence of same-type items months := [...]string {1:"Jan", 2:"Feb", /*...,*/ 12:"Dec" a slice is a subsequence of an array summer := months[6:9]; Q2 := months[4:7] elements accessed as slice[index] indices from 0 to len(slice)-1 inclusive summer[0:3] is elements months[6:9] summer[0] = "Juin" loop over a slice with for range for i, v := range summer { fmt.println(i, v) slices are very efficient (represented as small structures) most library functions work on slices

Arrays and slices

Maps (== associative arrays) unordered collection of key-value pairs keys are any type that supports == and!= operators values are any type // Find duplicated lines in stdin. func main() { counts := make(map[string]int) in := bufio.newscanner(os.stdin) for in.scan() { counts[in.text()]++ for line, n := range counts { if n > 1 { fmt.printf("%d\t%s\n", n, line)

Methods and pointers can define methods that work 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())

Interfaces 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 a type implements an interface merely by defining the required methods it doesn't declare that it implements them Writer: the most common interface type Writer interface { Write(p []byte) (n int, err error)

Example of Writer interface type ByteCounter int func (c *ByteCounter) Write(p []byte) (int, error) { *c += ByteCounter(len(p)) // convert int to ByteCounter return len(p), nil func main() { var c ByteCounter c.write([]byte("hello")) fmt.println(c) // "5", = len("hello") c = 0 // reset the counter var name = "Bob" fmt.fprintf(&c, "hello, %s", name) fmt.println(c) // "10", = len("hello, Bob")

Sort interface sort interface defines three methods any type that implements those three methods can sort algorithms are inside the sort package, invisible outside package sort type Interface interface { Len() int Less(i, j int) bool Swap(i, j int) // Len and Swap already defined for slices: sort.slice(sub, func(i, j int) bool { return sub[i].subname < sub[j].subname )

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)

Tiny web server func main() { http.handlefunc("/", handler) http.listenandserve("localhost:8000", nil) // handler echoes Path component of the request URL r. func handler(w http.responsewriter, r *http.request) { fmt.fprintf(w, "URL.Path = %q\n", r.url.path) http.responsewriter implements Writer interface

Tiny version of curl func main() { url := os.args[1] resp, err := http.get(url) if err!= nil { fmt.fprintf(os.stderr, "curl: %v\n", err) os.exit(1) _, err = io.copy(os.stdout, resp.body) if err!= nil { fmt.fprintf(os.stderr, "curl: copying %s: %v\n", os.exit(1) url, err)

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

Example: web crawler 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

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

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

Version 2: main() for parallelism with goroutines func main() { start := time.now() c := make(chan string) n := 0 for _, site := range os.args[1:] { n++ go count("http://" + site, 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(url string, c chan<- string) { start := time.now() r, err := http.get(url) if err!= nil { c <- fmt.sprintf("%s: %s\n", url, err) return n, _ := io.copy(ioutil.discard, r.body) r.body.close() dt := time.since(start).seconds() c <- fmt.sprintf("%s %d [%.2fs]\n", url, n, dt)

Python version, no parallelism import urllib2, time, sys def main(): start = time.time() for url in sys.argv[1:]: count("http://" + url) dt = time.time() - start print "\ntotal: %.2fs" % (dt) def count(url): start = time.time() n = len(urllib2.urlopen(url).read()) dt = time.time() - start print "%6d %6.2fs %s" % (n, dt, url) main()

Python version, with threads import urllib2, time, sys, threading global_lock = threading.lock() class Counter(threading.Thread): def init (self, url): super(counter, self). init () self.url = url def count(self, url): start = time.time() n = len(urllib2.urlopen(url).read()) dt = time.time() - start with global_lock: print "%6d %6.2fs %s" % (n, dt, url) def run(self): self.count(self.url) def main(): threads = [] start = time.time() for url in sys.argv[1:]: # one thread each w = Counter("http://" + url) threads.append(w) w.start() for w in threads: w.join() dt = time.time() - start print "\ntotal: %.2fs" % (dt) main()

Python version, with threads (main) def main(): threads = [] start = time.time() for url in sys.argv[1:]: # one thread each w = Counter("http://" + url) threads.append(w) w.start() for w in threads: w.join() dt = time.time() - start print "\ntotal: %.2fs" % (dt) main()

Python version, with threads (count) import urllib2, time, sys, threading global_lock = threading.lock() class Counter(threading.Thread): def init (self, url): super(counter, self). init () self.url = url def count(self, url): start = time.time() n = len(urllib2.urlopen(url).read()) dt = time.time() - start with global_lock: print "%6d %6.2fs %s" % (n, dt, url) def run(self): self.count(self.url)

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