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

Similar documents

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

Another Go at Language Design

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

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

Introduzione a Go e RPC in Go

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

The Go Programming Language. Frank Roberts

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

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

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

Go for Java Developers

Go Language September 2016

Jython. An introduction by Thinh Le

Overview. Rationale Division of labour between script and C++ Choice of language(s) Interfacing to C++ Performance, memory

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

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

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

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

Overview. Rationale Division of labour between script and C++ Choice of language(s) Interfacing to C++

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

New Parallel Programming Languages for Optimization Research

Using the Go Programming Language in Practice

CS 11 java track: lecture 1

David DeFlyer Class notes CS162 January 26 th, 2009

Remote Procedure Call

More Examples /

Go Tutorial. Arjun Roy CSE 223B, Spring 2017

CSE : Python Programming

Chapter 1 GETTING STARTED. SYS-ED/ Computer Education Techniques, Inc.

CS 11 Haskell track: lecture 1

understanding

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

CS558 Programming Languages

Using DPDK with Go. Takanari Hayama Technology Consulting Company IGEL Co.,Ltd.

Principles of Programming Languages. Lecture Outline

COMP322 - Introduction to C++ Lecture 01 - Introduction

Principles of Programming Languages. Objective-C. Joris Kluivers

Future Web App Technologies

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


CS558 Programming Languages

THE GO PROGRAMMING LANGUAGE CHARACTERISTICS AND CAPABILITIES

Python for Earth Scientists

Matt Butcher Matt Farina

CS 3813/718 Fall Python Programming. Professor Liang Huang.

CS252 Advanced Programming Language Principles. Prof. Tom Austin San José State University Fall 2013

These are notes for the third lecture; if statements and loops.

G Programming Languages Spring 2010 Lecture 6. Robert Grimm, New York University

INF5750. Introduction to JavaScript and Node.js

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

Lecture Notes on Programming Languages

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

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

Updating the Compiler?

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

Accelerating Information Technology Innovation

New Programming Paradigms

Short Notes of CS201

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

CS201 - Introduction to Programming Glossary By

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

CSC209: Software tools. Unix files and directories permissions utilities/commands Shell programming quoting wild cards files

CSC209: Software tools. Unix files and directories permissions utilities/commands Shell programming quoting wild cards files. Compiler vs.

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

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

GO IDIOMATIC CONVENTIONS EXPLAINED IN COLOR

Arrays and Strings

Piqi-RPC. Exposing Erlang services via JSON, XML and Google Protocol Buffers over HTTP. Friday, March 25, 2011

Smart Pointers, deleted functions, and 2-3 trees

Topic 1: Introduction

COMP-520 GoLite Tutorial

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

Intro. Scheme Basics. scm> 5 5. scm>

Technical Game Development II IMGD 4000 (D 11) 1. having agents follow pre-set actions rather than choosing them dynamically

CSE 374 Programming Concepts & Tools

CS558 Programming Languages

The Go Programming Language. Part 2

Richard Mallion. Swift for Admins #TEAMSWIFT

Functions & Variables !

CSC209 Review. Yeah! We made it!

Introduction. A. Bellaachia Page: 1

Tranquility Publications. Web Edition MAC

JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB PROGRAMMING, X452.1)

Principles of Computer Game Design and Implementation. Lecture 3

CMSC 330: Organization of Programming Languages

CSCI-GA Scripting Languages

the gamedesigninitiative at cornell university Lecture 7 C++ Overview

An Introduc+on to Computers and Java CSC 121 Spring 2017 Howard Rosenthal

The Design and Implementation of a Modern Lisp. Dialect

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

Introduction to the Go Programming Language

IMGD 4000 Technical Game Development II Scripting

The Pyth Language. Administrivia

CE221 Programming in C++ Part 1 Introduction

Making New Pseudo-Languages with C++

Exception Namespaces C Interoperability Templates. More C++ David Chisnall. March 17, 2011

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

Show and Tell. 1. Plan 9 Things (brief) 2. An Extensible Compiler for Systems Programming. Russ Cox 1127 Show and Tell April 19, 2005

GO SHORT INTERVIEW QUESTIONS EXPLAINED IN COLOR

Transcription:

Lecture 4: Golang Programming Lecturer: Prof. Zichen Xu

Outline The history The reason The code

Sophistication If more than one function is selected, any function template specializations in the set are eliminated if the set also contains a nontemplate function, and any given function template specialization F1 is eliminated if the set contains a second function template specialization whose function template is more specialized than the function template of F1 according to the partial ordering rules of 14.5.6.2. After such eliminations, if any, there shall remain exactly one selected function. (C++0x, 13.4 [4])

Sophistication Which Boost templated pointer type should I use? - linked_ptr - scoped_ptr - shared_ptr - smart_ptr - weak_ptr - intrusive_ptr - exception_ptr

Noise public static <I, O> ListenableFuture<O> chain(listenablefuture<i> input, Function<? super I,? extends ListenableFuture<? extends O>> function) - a recently observed chat status foo::foo *myfoo = new foo::foo(foo::foo_init) - but in the original Foo was a longer word

How did we get here? 1) C and Unix became dominant in research. 2) The desire for a higher-level language led to C++, which grafted the Simula style of object-oriented programming onto C. It was a poor fit but since it compiled to C it brought high-level programming to Unix. 3) C++ became the language of choice in parts of industry and in many research universities. 4) Java arose as a clearer, stripped-down C++. 5) By the late 1990s, a teaching language was needed that seemed relevant, and Java was chosen.

Programming becomes too hard These languages are hard to use. They are subtle, intricate, and verbose. Their standard model is oversold, and we respond with add-on models such as "patterns". (Norvig: patterns are a demonstration of weakness in a language.) Yet these languages are successful and vital

Reaction The inherent clumsiness of the main languages has caused a reaction. A number of successful simpler languages (Python, Ruby, Lua, JavaScript, Erlang,...) have become popular, in part as a rejection of the standard languages. Some beautiful and rigorous languages designed by domain experts (Scala, Haskell,...) have also arisen, although they are not as widely adopted. So despite the standard model, other approaches are popular and there are signs of a growth in "outsider languages, a renaissance of language invention

Confusion The standard languages (Java, C++) are statically typed. Most outsider languages (Ruby, Python, JavaScript) are interpreted and dynamically typed. Perhaps as a result, non-expert programmers have confused "ease of use" with interpretation and dynamic typing. This confusion arose because of how we got here: grafting an orthodoxy onto a language that couldn't support it cleanly.

The Good The standard languages are very strong: type-safe, effective, efficient. In the hands of experts, they are great. Huge systems and huge companies are built on them. In practice they work well for large scale programming: big programs, many programmers.

The bad The standard languages are hard to use. Compilers are slow and fussy. Binaries are huge. Effective work needs language-aware tools, distributed compilation farms,... Many programmers prefer to avoid them. The languages are at least 10 years old and poorly adapted to the current computing environment: clouds of networked multicore CPUs.

And Python This is partly why Python et al. have become so popular: They don't have much of the "bad". - dynamically typed (fewer noisy keystrokes) - interpreted (no compiler to wait for) - good tools (interpreters make things easier) But they also don't have the "good": - slow - not type-safe (static errors occur at runtime) - very poor at scale And they're also not very modern.

A niche There is a niche to be filled: a language that has the good, avoids the bad, and is suitable to modern computing infrastructure: - comprehensible - statically typed - light on the page - fast to work in - scales well - doesn't require tools, but supports them well - good at networking and multiprocessing

Go developed ~2007 at Google by Robert Griesemer, Rob Pike, Ken Thompson open source 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

Negative influences "When the three of us got started, it was pure research. The three of us got together and decided that we hated C++. We started off with the idea that all three of us had to be talked into every feature in the language, so there was no extraneous garbage put into the language for any reason." (Ken Thompson)

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

Hello, world 2.0 package main import ( "fmt" "http" ) func handler(c *http.conn, r *http.request) { fmt.fprintf(c, "Hello, %s.", r.url.path[1:]) func main() { http.listenandserve(":8080", http.handlerfunc(handler))

What does Go do? Fast compilation Expressive type system Concurrency Garbage collection Systems programming capabilities Clarity and orthogonality

Go s Performance New clean compiler worth ~5X compared to gcc. We want a millionx for large programs, so we need to fix the dependency problem. In Go, programs compile into packages and each compiled package file imports transitive dependency info. If A.go depends on B.go depends on C.go: - compile C.go, B.go, then A.go. - to compile A.go, compiler reads B.o but not C.o. At scale, this can be a huge speedup.

Trim the tree Large C++ programs (Firefox, OpenOffice, Chromium) have huge build times. On a Mac (OS X 10.5.7, gcc 4.0.1): - C: #include <stdio.h> reads 360 lines from 9 files - C++: #include <iostream> reads 25,326 lines from 131 files - Objective-C: #include <Cocoa/Cocoa.h> reads 112,047 lines from 689 files But we haven't done any real work yet! In Go, import "fmt" reads one file: 195 lines summarizing 6 dependent packages. As we scale, the improvement becomes exponential.

Go is different Go is object-oriented not type-oriented inheritance is not primary methods on any type, but no classes or subclasses Go is (mostly) implicit not explicit types are inferred not declared objects have interfaces but they are derived, not specified Go is concurrent not parallel intended for program structure, not max performance but still can keep all the cores humming nicely... and many programs are more nicely expressed with concurrent ideas even if not parallel at all

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 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)

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] = "Jun" 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

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)

Sort interface sort interface defines three methods any type that implements those three methods can sort algorithms are inside the soft package, invisible outside package sort type Interface interface { Len() int Less(i, j int) bool 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)

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", url, err) os.exit(1)

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

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 much 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

Practical Use Cases The Really Good: Network and Web Servers Network applications live and die by concurrency Go's native concurrency features: (goroutines and channels) Networking, distributed functions, or services (APIs, Web Servers, minimal frameworks for Web Apps) Items are either a part of language (goroutines for threadlike behavior) or available in the standard library http package. Batteries included" philosophy like Python Stand-alone command-line apps or scripts Stand-alone executables with no external dependencies Versus Python which requires a copy of the interpreter on the target machine Can talk to underlying system, external C libraries, native system calls Caution: Desktop/GUI-based apps & System-Level Programming 37

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

Conclusion We have started on Golang programming We have shared the history of a relatively new programming language We have discussed the syntax of Golang programming We will start on System Functionality next week

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)