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

Similar documents
The Go Programming Language. Frank Roberts

Using the Go Programming Language in Practice

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


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

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

Introduzione a Go e RPC in Go

Go Language September 2016

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

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

Go for Java Developers

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

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

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

GO IDIOMATIC CONVENTIONS EXPLAINED IN COLOR

Embedded type method, overriding, Error handling, Full-fledged web framework, 208 Function defer, 31 panic, 32 recover, 32 33

Go Tutorial. Arjun Roy CSE 223B, Spring 2017

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

New Parallel Programming Languages for Optimization Research

Matt Butcher Matt Farina

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

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

CS Prof J.P.Morrison

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

Updating the Compiler?

William Kennedy. Brian Ketelsen Erik St. Martin Steve Francia FOREWORD BY MANNING WITH

Jython. An introduction by Thinh Le

WORKSHOP: from Zero to a Network Application with #golang

Introduction to the Go Programming Language

The pkgsrc wrapper framework

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

understanding

Processes. Johan Montelius KTH

A process. the stack

Table of Contents EVALUATION COPY

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

CSE506: Operating Systems CSE 506: Operating Systems

Overview of the Ruby Language. By Ron Haley

Another Go at Language Design

Tranquility Publications. Web Edition MAC

Operating Systems CMPSCI 377, Lec 2 Intro to C/C++ Prashant Shenoy University of Massachusetts Amherst

Grade Weights. Language Design and Overview of COOL. CS143 Lecture 2. Programming Language Economics 101. Lecture Outline

Accelerating Information Technology Innovation

Accelerating Information Technology Innovation

Concurrency, Thread. Dongkun Shin, SKKU

Go at Canonical. Transitioning Juju to Go. Presentation by Dave Cheney juju.ubuntu.com August 2012

Motivation. Threads. Multithreaded Server Architecture. Thread of execution. Chapter 4

CSE 504: Compiler Design. Runtime Environments

p k g _ s e l e c t : s o m a n y p a c k a g e s, s o f e w c o l u m n s

Transitioning from Spread 3.17.x to Spread 4.0

Chapter 3: Operating-System Structures

Programming in C - Part 2

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

Future Web App Technologies

CSCI-GA Scripting Languages

pkgsrc for users and developers

Problem Set: Processes

Operating System Labs. Yuanbin Wu

swiftenv Documentation

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

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

Software Development & Education Center. Java Platform, Standard Edition 7 (JSE 7)

pkgsrc on MirBSD Benny Siegert FOSDEM 2012

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

Chapter 2: Operating-System Structures. Operating System Concepts 9 th Edit9on

CS 5220: Shared memory programming. David Bindel

Chapter 2: Operating-System Structures

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

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

CISC2200 Threads Spring 2015

go-tutorial Documentation Release latest

A Tour of Versioned Go (vgo) Go & Versioning, Part 2

Report.doc General design overview of the software Manual.doc How to use the software

The Go Programming Language. Part 3

Seminar on Languages for Scientific Computing Aachen, 6 Feb Navid Abbaszadeh.

Chapter 3: Operating-System Structures

Forth Meets Smalltalk. A Presentation to SVFIG October 23, 2010 by Douglas B. Hoffman

Chapter 2: Operating-System Structures. Operating System Concepts 9 th Edition

Problem Set: Processes

The Case for Building a Kernel

CMSC 330: Organization of Programming Languages

Maintain the NetBSD Base System Using pkg_* Tools. Yuuki Enomoto Ken ichi Fukamachi Chitose Institute of Science and Technology

GO - QUICK GUIDE GO - OVERVIEW

Outline. Threads. Single and Multithreaded Processes. Benefits of Threads. Eike Ritter 1. Modified: October 16, 2012

The current topic: Python. Announcements. Python. Python

The benefits and costs of writing a POSIX kernel in a high-level language

OPERATING SYSTEM. Chapter 4: Threads

How we use Go to build APIs PubNative best practices and patterns. Kostiantyn Stepaniuk PubNative GmbH

OS and Computer Architecture. Chapter 3: Operating-System Structures. Common System Components. Process Management

Bazaar Architecture Overview Release 2.8.0dev1

Synchronization SPL/2010 SPL/20 1

Microservices with Node.js

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

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

Chapter 3: Operating-System Structures

Advanced use of the C language

All code must follow best practices. Part (but not all) of this is adhering to the following guidelines:

COMP-520 GoLite Tutorial

Lecture 1: Overview of Java

ECS 150 Operating Systems

Transcription:

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

Agenda What is Go? Building Go code with the gotool Running Go code pkgsrc Conclusion

What is Go?

A modern systems programming language Initially developed at Google, open source since 2009. Initial implementation by Rob Pike, Robert Griesemer, Russ Cox, Ken Thompson. compiled mostly statically typed garbage collected provides control over memory layout provides access to C APIs (via cgo) and syscalls Go has powerful concurrency primitives.

Go is: Simple: concepts are easy to understand (the implementation might still be sophisticated) Orthogonal: concepts mix clearly easy to understand and predict what happens Succinct: no need to predeclare every intention Safe: misbehavior should be detected These combine to give expressiveness. (Source: R. Pike, The Expressiveness of Go (2010), http://talks.golang.org/2010/expressivenessofgo-2010.pdf (http://talks.golang.org/2010/expressivenessofgo-2010.pdf) )

Clean The language is defined by a short and readable specification. Read it. implemented by two compilers: gc and gccgo (gcc frontend). The APIs in the standard library are well thought out, contrary to the "bureaucracy" of C++ or Java: foo::foo*myfoo = new foo::foo(foo::foo_init) but in the original Foo was a longer word The standard library has "batteries included". The code has a standard formatting, enforced by gofmt. No more discussions about braces and indentation!

Hello World package main import"fmt" funcmain(){ fmt.println("hello World!") } Run All code lives in a package (package main is a command). Semicolons are inserted automatically. Opening brace for functions must go on the same line. Strings are UTF-8, built-in string data type.

Another Hello World package main import( "flag" "fmt" "net/http" ) var addr*string = flag.string("addr",":8080","host:port to listen on") funcmain(){ flag.parse() http.handlefunc("/", func(w http.responsewriter, r*http.request){ fmt.fprintln(w,"hello World!") }) http.listenandserve(*addr, nil) } Run net/httpis not a toy web server! It powers e.g. dl.google.com.

Basic data structures: slices and maps Slices are a form of dynamic arrays. a:=[]int{1,2,3,4}//len(a)=4,cap(a)=4 b:=a[2:4] //b[0]=3,b[1]=4 b=append(b,5)//b[2]=5 _ = b[3] // out-of-bounds access Run Strings are immutable; they can be converted to []byteor []rune. Type-safe hashtables (maps) are built-in. translations:= make(map[string]string) translations["hello"] ="Bonjour"

Object orientation Objects in Go do not work like they do in C++. No inheritance, no polymorphy. They are more similar to objects in Perl 5. You start from a basic type (struct, int, string,...) and add methods. package foo type Number int func(n Number) Square() Number{ returnn*n } Methods have a receiver before the name (often a pointer).

Table-driven testing package foo import"testing" var squaretests =[]struct{ num, square Number }{ {1,1}, {2,4}, {256, 65536}, {-10,100}, } func TestSquare(t*testing.T){ for_,test:=rangesquaretests{ actual:= test.num.square() if actual!= test.square{ t.errorf("square() of%v: got%v, want%v", test.num, actual, test.square) } } }

Table-driven tests (2) Here is the test run: $gotest PASS ok github.com/bsiegert/talks/go-netbsd/object 0.004s If I deliberately insert a mistake: $gotest --- FAIL: TestSquare(0.00 seconds) object_test.go:21: Square() of-10: got 100, want-100 FAIL exit status 1 FAIL github.com/bsiegert/talks/go-netbsd/object 0.004s Finally, useful diagnostics!

Interfaces Interfaces work on methods, not on data. type Reader interface{ Read(p[]byte)(n int, err error) } type Writer interface{ Write(p[]byte)(n int, err error) } type ReadWriter interface{ Reader Writer } Any type that implements these methods fulfills the interface implicitly (i.e. no "implements" declarations). Use the interface instead of a concrete type, e.g. in a function: func Fprintf(w io.writer, format string, a...interface{})(n int, err error)

Concurrency: goroutines A goroutine is a sort of lightweight thread. It runs in the same address space, concurrently and independent from the other goroutines. f("helloworld")//fruns;wewait go f("hello World")// execution continues while f is running They are much cheaper than threads, you can have thousands of them. If one goroutine blocks (e.g. on I/O), the others continue to run. This is easier to reason about than I/O with callbacks, as in node.js. Maximum number of goroutines running in parallel is configurable (e.g. one per core).

Concurrency: channels Channels are type-safe "pipes" to transfer data between goroutines. "Don't communicate by sharing memory -- share memory by communicating." They are also a synchronization point. timer:= make(chan bool) gofunc(){ time.sleep(deltat) timer <- true }() // Do something else; when ready, receive. // Receive will block until timer delivers. <-timer Easily implement worker pools, parallelize computations, etc. More information: R. Pike, "Concurrency is not parallelism", http://talks.golang.org/2012/waza.slide (http://talks.golang.org/2012/waza.slide).

"Self-documenting" code: godoc godoc extracts and generates documentation for Go programs, using comments in the source code. // Package strings implements simple functions to manipulate strings. package strings // Count counts the number of non-overlapping instances of sep in s. func Count(s, sep string) int{ // } http://golang.org (http://golang.org) runs godoc on Google App Engine. godoc-http=:6060runs the server locally. godoc fooshows the documentation on the console (similar to a manpage). Commands often have a doc.go containing only documentation.

Building Code With the go Tool

GOROOT and GOPATH The default build tool is called go. It uses $GOROOTand $GOPATH. GOROOT contains the standard Go tree (source + compiled form). GOPATH is a colon-separated list of "user paths". It must be set by the developer. Even after building, the source code is needed for godoc and for building dependent packages.

GOPATH example GOPATH=/home/user/gocode /home/user/gocode/ src/ myproject/ foo/ (go code in package foo) x.go server/ (go code in package main) y.go bin/ server (installed command) pkg/ netbsd_amd64/ myproject/ foo.a (installed package object)

Conventions for remote repos package main import( "code.google.com/p/go.image/tiff" "github.com/mattn/go-gtk" "launchpad.net/goamz/ec2" ) Import path == URL (more or less). Supports free hosters and custom remote repositories. gogetgithub.com/user/repo/packageinstalls these dependencies (if you have git, hg, bzr installed). fetch, build, install supports recursive fetching of dependencies

Running Go code

A word on compilers The gc suite is the most often used compiler suite. It compiles insanely fast. supports i386, amd64, arm Linux, FreeBSD, OpenBSD, NetBSD, Windows easy to cross-compile for other platforms gccgo is a Go frontend for gcc, included in gcc 4.7.x. supports all platforms gcc supports better optimizations may not have the latest standard libraries has fewer users

Go packages All Go code lives in a package. Compiling a package main produces an executable binary. Other packages are compiled to static libraries (.a files). contain code and the exported interface contain all dependencies.a files from different compilers (and different compiler versions) are incompatible.

Running a server written in Go Currently, Go programs cannot daemonize, so they run in the foreground. daemonize is hard to implement My suggestion: run it under daemontools (sysutils/daemontools) log on stdout, collect logs with multilog

Daemontools example: continuous builder /service/builder/run contains: #!/bin/sh exec 2>&1 exec setuidgid builder envdir env/service/builder/builder-commit netbsd-amd64-bsiegert /service/builder/env/home contains: /home/builder /service/builder/log/run contains: #!/bin/sh exec setuidgid buildlog multilog./main Just copy the executable to /service/builder/builder, done!

pkgsrc

wip/go Go is available in pkgsrc-wip as wip/go. It installs source + binaries to $PREFIX/go. The package supports NetBSD Linux OpenBSD (untested) Mac OS X (currently broken) on i386 and x86_64. NetBSD support is not in a stable release yet. (Go 1.1 will have it.)

pkgsrc and software written in Go Two cases: libraries and executables. Executables are easy, as they are statically linked. may link dynamically against C libraries no runtime dependencies to Go libs need source (or part of it) for godoc

Libraries Libraries have to be recompiled each time you upgrade the Go compiler. similar to OCaml make binary packages depend on the exact compiler version recursive revbump when compiler is updated We will need a go/package.mk. Due to the way GOPATH work, adding buildlink should be easy symlinks or: install each library in a distinct path, add its directory to the GOPATH used for the build from buildlink3.mk.

Conclusion

Conclusion Try Go! It may not look very revolutionary on first glance, but it is addictive. Try Go on NetBSD. Try wip/go and report any problems.

Thank you Benny Siegert Google Switzerland; The NetBSD Foundation (mailto:bsiegert@google.com) (mailto:bsiegert@netbsd.org) (http://www.mirbsd.org/wlog-10.htm)