The Go Programming Language. Frank Roberts

Similar documents
New Parallel Programming Languages for Optimization Research

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

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

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

Go for Java Developers

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

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

Introduzione a Go e RPC in Go

What Came First? The Ordering of Events in

Go Language September 2016

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

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


Advanced Systems Programming

GO SHORT INTERVIEW QUESTIONS EXPLAINED IN COLOR

The current topic: Python. Announcements. Python. Python

Topic 6: A Quick Intro To C

Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

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

GO CONCURRENCY BASICS AND PATTERNS EXPLAINED IN COLOR

GO MOCK TEST GO MOCK TEST I

Inter-process communication (IPC)

THE GO PROGRAMMING LANGUAGE CHARACTERISTICS AND CAPABILITIES

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

Why do we care about parallel?

The Go Programming Language. Part 3

Run-Time Data Structures

Q1: /8 Q2: /30 Q3: /30 Q4: /32. Total: /100

COMP 202 Java in one week

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.

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

LFE - a lisp on the Erlang VM

Linux Systems Administration Shell Scripting Basics. Mike Jager Network Startup Resource Center

CS 5523 Operating Systems: Midterm II - reivew Instructor: Dr. Tongping Liu Department Computer Science The University of Texas at San Antonio

Using the Go Programming Language in Practice

UNIT I Programming Language Syntax and semantics. Kainjan Sanghavi

CSci 4061 Introduction to Operating Systems. Programs in C/Unix

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

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

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

Hello, World! in C. Johann Myrkraverk Oskarsson October 23, The Quintessential Example Program 1. I Printing Text 2. II The Main Function 3

CSC209 Review. Yeah! We made it!

Chapter 3 Process Description and Control

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

Compiling Parallel Algorithms to Memory Systems

Intel Threading Tools

Concurrent Programming. Copyright 2017 by Robert M. Dondero, Ph.D. Princeton University

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

CS390 Principles of Concurrency and Parallelism. Lecture Notes for Lecture #5 2/2/2012. Author: Jared Hall

Dynamic Programming. See p of the text

Motivation was to facilitate development of systems software, especially OS development.

High Performance Computing Prof. Matthew Jacob Department of Computer Science and Automation Indian Institute of Science, Bangalore

Problem Set: Processes

4.8 Summary. Practice Exercises

Problem Set: Processes

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Lecture 2: Variables & Assignments

CS 455 Final Exam Fall 2015 [Bono] Dec. 15, 2015

Operating Systems 2 nd semester 2016/2017. Chapter 4: Threads

Processes in linux. What s s a process? process? A dynamically executing instance of a program. David Morgan. David Morgan

CMSC330 Fall 2010 Final Exam Solutions

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

C - Basics, Bitwise Operator. Zhaoguo Wang

Recap: Functions as first-class values

Lecture 10: Potpourri: Enum / struct / union Advanced Unix #include function pointers

Operating Systems: Internals and Design Principles. Chapter 4 Threads Seventh Edition By William Stallings

Lecture 03 Bits, Bytes and Data Types

Don t let data Go astray

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

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

CS 470 Spring Mike Lam, Professor. OpenMP

understanding

1 Process Coordination

About this exam review

Jython. An introduction by Thinh Le

NODE.JS SERVER SIDE JAVASCRIPT. Introduc)on Node.js

QUIZ on Ch.5. Why is it sometimes not a good idea to place the private part of the interface in a header file?

Laboratory Assignment #3. Extending scull, a char pseudo-device

Design Issues. Subroutines and Control Abstraction. Subroutines and Control Abstraction. CSC 4101: Programming Languages 1. Textbook, Chapter 8

Starting the System & Basic Erlang Exercises

QUIZ. What are 3 differences between C and C++ const variables?

TDDE18 & 726G77. Functions

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

Motivation was to facilitate development of systems software, especially OS development.

CS558 Programming Languages

Richard Mallion. Swift for Admins #TEAMSWIFT

Erlang. Joe Armstrong.

Scala, Your Next Programming Language

A Small Interpreted Language

CS 11 C track: lecture 6

PIC 10A. Lecture 17: Classes III, overloading

COMP-520 GoLite Tutorial

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

SYSC 2006 C Winter String Processing in C. D.L. Bailey, Systems and Computer Engineering, Carleton University

CMSC 330: Organization of Programming Languages

Chapter 9 :: Data Abstraction and Object Orientation

CS 220: Introduction to Parallel Computing. Beginning C. Lecture 2

Processor. Lecture #2 Number Rep & Intro to C classic components of all computers Control Datapath Memory Input Output

Stanford University Computer Science Department CS 295 midterm. May 14, (45 points) (30 points) total

Transcription:

The Go Programming Language Frank Roberts frank.roberts@uky.edu

- C++ (1983), Java (1995), Python (1991): not modern - Java is 18 years old; how has computing changed in 10? - multi/many core - web programming is everywhere - massive parallel and distributed systems - These languages are not designed for today's environment - Google designed Go to deal with shortcomings of current systems-level languages - Go is designed to make writing code on modern systems easier and more natural.

- What makes Go modern? - Maps and slices are built in. - Garbage collection is built in. - Concurrency is built in. - What makes Go better? - Good design choices simplify the language. - A new approach to encapsulation - A better concurrency model

1 package main 2 3 import "fmt" 4 5 func main() { 6 7 fmt.print("hello, World.\n") 8 9 }

- Slices and Maps are built in flexible structures. - Slices - More flexible than arrays - Similar to lists in Python - Support for slicing operations: myslice[start:end]

1 func main() { 2 fib := []int{0, 1, 1, 2, 3, 5, 8, 13} 3 fmt.println(fib[3]) 4 fmt.println(fib[5:7]) 5 fmt.println(fib[:3]) 6 fib = append(fib, 21) 7 fmt.println(fib[3:]) 8 } Output: 2 [5 8] [0 1 1] [2 3 5 8 13 21]

- Maps - Associate keys with values - Keys may be almost any type (== must be defined) - simple literal syntax - fetch of non-existent key results in zero value - Compose slices and maps for simple data structures

1 func main() { 2 attended := map[string] bool{ 3 "Ann": true, 4 "Joe": true} 5 fmt.println(attended["ann"]) 6 fmt.println(attended["bill"]) 7 present, ok := attended["paul"] 8 fmt.println(present, ok) 9 } Output: true false false false

- Concurrency model: Share memory by communicating - Goroutines - More lightweight than threads - Say go foo() to run foo concurrently - Similar to backgrounding in a Linux shell with '&' - Channels - Like Unix pipes - channels are typed - Programmer has full control over buffering - May be of any type, including channels - Structure concurrency so that synchronization is implicit in the communication patterns.

- Example: Testing to find prime numbers - Use a manager-worker model - Manager spawns a number of testing routines - Each routine tests a different portion of the range - Testers send primes to manager over a single channel - Testers send a flag value over channel before exiting - Manager collects primes as they are comupted - Manager sorts and prints list

The testing routine: 1 package main 2 3 func test_range(start, stop, step int, res chan int) { 4 5 for i := start; i < stop; i += step { 6 prime := true 7 if i % 2 == 0 && i!= 2 { prime = false } 8 for j := 3; j*j <= i && prime; j += 1 { 9 if i % j == 0 { 10 prime = false 11 } 12 } 13 if prime {res <- i} 14 } 15 res <- 0 16 }

Spawn goroutines: 15 runtime.gomaxprocs(ncpu) 16 17 res := make(chan int, buf) 18 for i := 0; i < NCPU; i++ { 19 go test_range(i+1, end, NCPU, res) 20 }

Collect prime numbers into a slice: 29 alldone := 0 30 for alldone < NCPU { 31 next = <-res 32 if next!= 0 { 33 primes = append(primes, next) 34 } else { 35 alldone += 1 36 } 37 }

-Reading and constructing types - Reads left to right always English declaration C declaration Go declaration

-Reading and constructing types - Reads left to right always English declaration C declaration Go declaration declare foo as array 10 of int int foo[10] var foo [10]int

-Reading and constructing types - Reads left to right always English declaration C declaration Go declaration declare foo as array 10 of int int foo[10] var foo [10]int declare foo as array of pointer to int int *foo[] var foo []*int

-Reading and constructing types - Reads left to right always English declaration C declaration Go declaration declare foo as array 10 of int int foo[10] var foo [10]int declare foo as array of pointer to int declare foo as array of pointer to function returning int int *foo[] int (*foo[])() var foo []*int var foo []func () int

-Reading and constructing types - Reads left to right always English declaration C declaration Go declaration declare foo as array 10 of int int foo[10] var foo [10]int declare foo as array of pointer to int declare foo as array of pointer to function returning int int *foo[] int (*foo[])() var foo []*int var foo []func () int int (*(*foo)(int (*)(int, int ), int ))(int, int ) var foo func(func(int, int) int, int) func(int, int) int declare foo as pointer to function (pointer to function (int, int) returning int, int) returning pointer to function (int, int) returning int

- Dependency Analysis - Poor dependency analysis hurts compile time - C-style includes are difficult to analyze at compile time - include guards don't prevent extra reads - Example: KOAP my own 1200 line C++ project - includes 129 headers 837 times total - top-level C++ file includes 122 headers 149 times - Example: Google binary (instrumented in 2007) - Opens hundreds of headers tens of thousands of times - 4.2MB of source expands to 8GB - Builds take approximately half an hour on a distributed build system

- Go defines dependencies as part of the language - The dependencies of a Go package are always computable - Circular dependencies are not permitted - imports for unused packages are compilation errors - Go's dependency model isn't new - The Go compiler spends less time reading dependencies - No more than one file read per import - Export info goes at the top of a compiled package - Google instrumented the build of large Go program - Code fanout is 50x better than the C++ example - Builds take seconds, not minutes

- What I didn't mention - Go takes a new and better approach to encapsulation - Go has: - First class function values - A large standard library - A tool for building, analyzing, testing, documenting, formating, and fixing code - Even more little things... - Why use Go? - Modern features in a compiled language - Go is fun to write

References and Resources: - Effective Go: http://golang.org/doc/effective_go.html - The Go Programming Language: http://golang.org - Go at Google: Language Design in the Service of Software Engineering: http://talks.golang.org/2012/splash.article - The Go Programming Language Specification: http://golang.org/ref/spec - Go Playground: http://play.golang.org - A Tour of Go: http://tour.golang.org Me: www.jafrro.net, frank@jafrro.net