Using the Go Programming Language in Practice Erik Westrup & Fredrik Pettersson Department of Computer Science, Lund University Axis Communications, Sweden May 28, 2014 Supervisors: Jonas Skeppstedt <jonas.skeppstedt@cs.lth.se> (also Examiner) Mathias Bruce <mathias.bruce@axis.com> Robert Rosengren <robert.rosengren@axis.com>
Authors Erik Westrup <erik.westrup@gmail.com> M.Sc. in Computer Science & Engineering, Lund University 2009 From: Ljunghusen, Sweden $EDITOR=vim $BROWSER=firefox+pentadactyl colorscheme=solarized Fredrik Pettersson <fredrik.pettersson.89@gmail.com> M.Sc. in Computer Science & Engineering, Lund University 2009 From: Halmstad, Sweden $EDITOR=sublime2 $BROWSER=chromium colorscheme=solarized
Table of Contents Introduction Approach Discussion Conclusions
Introduction
Outline Introduction to Go Designed by: Ken Thompson, Rob Pike et al. at Google Appeared in 2009 Open source
Outline Introduction to Go Designed by: Ken Thompson, Rob Pike et al. at Google Appeared in 2009 Open source
Outline Introduction to Go Designed by: Ken Thompson, Rob Pike et al. at Google Appeared in 2009 Open source Meets large-scale dev. problems including: long build times uncontrolled dependencies hard to understand code
Outline Purpose & Goals Review of Go + tools Go for embedded programming? How to review a language?
Outline Problem Formulation
Outline Problem Formulation Go Easy to learn? Mature? Future?
Outline Problem Formulation Go Easy to learn? Mature? Future? Building & Compiling Easy? Fast? Avoid tools like Makefiles? Cross-compilation C-integration?
Outline Problem Formulation Go Easy to learn? Mature? Future? Development tools? Software Product & Development Qualities? Building & Compiling Easy? Fast? Avoid tools like Makefiles? Cross-compilation C-integration?
Approach
The Go Programming Language Language design goals
The Go Programming Language Language design goals Features Simple specification Easy to understand code C-like syntax Compiled & statically typed Type inference
The Go Programming Language Language design goals Features Simple specification Easy to understand code C-like syntax Compiled & statically typed Type inference OOP: interfaces, structs & composition Concurrency: goroutines & channels Garbage collection
The Go Programming Language Language design goals Features Simple specification Easy to understand code C-like syntax Compiled & statically typed Type inference OOP: interfaces, structs & composition Concurrency: goroutines & channels Garbage collection Lack of Features Type-orientation: inheritance Generics Exceptions Fat standard library Pointer arithmetic
The Go Programming Language Hello, world 1 package main 2 3 import " fmt " 4 5 func main ( ) { 6 fmt. P r i n t l n ( " Hello, World! " ) 7 }
The Go Programming Language Types 1 v a r i i n t 2 v a r s [ ] b o o l 3 v a r m map [ i n t ] s t r i n g 4 f := 3.14
The Go Programming Language Declaration order C, inside-out: t y p e d e f char ( ( f u n c _ c o l l e c t i o n [ 1 ] ) ) ( i n t ) ;
The Go Programming Language Declaration order C, inside-out: t y p e d e f char ( ( f u n c _ c o l l e c t i o n [ 1 ] ) ) ( i n t ) ; Go, left-to-right: type f u n c C o l l e c t i o n [ 1 ] ( func ( i n t ) s t r i n g )
The Go Programming Language Multiple return values & errors 1 func check ( i n p u t s t r i n g ) ( r e s u l t s t r i n g, e r r e r r o r ) { 2 e r r =... 3 r e s u l t =... 4 r e t u r n 5 }
The Go Programming Language Structs & Method receivers 1 type Object s t r u c t { 2 name s t r i n g 3 } 4 5 func ( o Object ) S t r i n g ( ) s t r i n g { 6 r e t u r n fmt. S p r i n t f ( " Object %s ", o. name ) 7 }
The Go Programming Language Interfaces 1 type S t r i n g e r i n t e r f a c e { 2 S t r i n g ( ) s t r i n g 3 }
The Go Programming Language Composition with struct-embedding 1 type B s t r u c t { 2 bdata i n t 3 } 4 5 type A s t r u c t { 6 adata i n t 7 B 8 } 9 10 func ( b B) o p e r a t e ( ) { 11 }
Concurrency goroutines Lightweight threads Multiplexed into OS threads go work ( )
Concurrency Channels For goroutine communication Declared for a given type Synchronized or asynchronized Replaces semaphores/mutexes 1 v a r c h a n n e l chan i n t 2 c h a n n e l < 8 // Send 3 i := < c h a n n e l // R e c e i v e
Developer tools Go tools $ go h e l p Go i s a t o o l f o r managing Go s o u r c e code. Usage : go command [ arguments ] The commands a r e : b u i l d c o m p i l e packages & d e p e n d e n c i e s fmt run gofmt on package s o u r c e s get download & i n s t a l l packages & d e p e n d e n c i e s t e s t t e s t packages......
Developer tools Building Easy to use Automated Smart dependency resolution The environment $ go b u i l d <package>
Developer tools gc vs gccgo
Developer tools gc vs gccgo gc (Go Compiler) Default compiler Ideas from Plan 9 compilers OSes: Linux, FreeBSD, OS X, Windows Architectures: i386, amd64, arm
Developer tools gc vs gccgo gc (Go Compiler) Default compiler Ideas from Plan 9 compilers OSes: Linux, FreeBSD, OS X, Windows Architectures: i386, amd64, arm gccgo Front-end for GCC OSes: Linux, BSD, OS X, Windows, IRIX, Solaris Architectures: i386, x86_64, amd64, arm, arm64, mips, alpha, m68k, powerpc, sparc
Developer tools Cross-compiling with gccgo Building x-gccgo As easy as building x-compiler for C
Developer tools Cross-compiling with gccgo Building x-gccgo As easy as building x-compiler for C Building x-compiler is very challenging
Developer tools Cross-compiling with gccgo Building x-gccgo As easy as building x-compiler for C Building x-compiler is very challenging Helper tools currently problematic Built a 4.9 GCC x-toolchain.
Developer tools Cross-compiling with gccgo Building x-gccgo As easy as building x-compiler for C Building x-compiler is very challenging Helper tools currently problematic Built a 4.9 GCC x-toolchain. Go + gccgo Unresolved issue
Developer tools C-Integration Tool: cgo Go-code: C-code & compiler directives in comments Can use C-symbols Can convert C Go types C-code: Can use Go-symbols
Developer tools C-Integration Tool: cgo Go-code: C-code & compiler directives in comments Can use C-symbols Can convert C Go types C-code: Can use Go-symbols Problems: Callbacks, pointers, macros etc.
Developer tools Debugging GDB works, bad goroutine & core dump support Uncertain GDB future Go-specific debugger?
Developer tools Other tools Many good tools exists: Built-in: Testing Documentation Code formatter Package manager Text editors & IDEs Convenient tools
Discussion
The Go Programming Language Design Goals Less is exponentially more Rob Pike
The Go Programming Language How did it turn out? U Well afterthought language d Flexible OOP U UGenerics: Necessary? Awesome concurrency model
Developer Tools Building U D Zero project startup cost U Less control & understandability D Better in the long run Warnings = Errors
Developer Tools gc vs gccgo
Developer Tools gc vs gccgo gc U Up-to-date Fast D Few architectures Only static linking
Developer Tools gc vs gccgo gc U Up-to-date Fast D Few architectures Only static linking U gccgo Supports many platforms D Slower U Dynamic linking D Different release schedule
Developer Tools Cross-Compilation D Uncertain Go + gccgo combination LLVM is the future?
Developer Tools C-Integration U Works well in simple cases Code in comments Locating errors D Wrapper-functions Slower builds
Developer Tools Other Tools U Simplifies development D No version support Package manager U D Testing & documentation State of debugging
Community & Future Community U Contributes to Go & tools U Friendly & helpful U Lead developers participate
Community & Future Future of Go U Stabilization/optimization of tools D Embedded programming: mild interest
Community & Future Future of Go U Stabilization/optimization of tools D Embedded programming: mild interest Google goes, Go goes? Competing languages?
Reviewing a Programming Language Worth Doing a Project?
Reviewing a Programming Language Worth Doing a Project? Yes! (if relevant & restricted) Reveals the true nature
Conclusions
Summary Summary U Easy to learn & adopt U Suitable for large-scale projects U Concurrency
Summary Summary Easy to learn & adopt U Suitable for large-scale projects Concurrency D Complicated C-integration U Cross-compilation with go + gccgo Reviewing languages larger project
Summary Summary Easy to learn & adopt U Suitable for large-scale projects Concurrency D Complicated C-integration U Cross-compilation with go + gccgo Reviewing languages larger project Go for Embedded? Wait a while
Summary Summary Easy to learn & adopt U Suitable for large-scale projects Concurrency D Complicated C-integration U Cross-compilation with go + gccgo Reviewing languages larger project Go for Embedded? Wait a while
Future Research Future Research Effectiveness of goroutines gc & gccgo optimization & compilation speed comparison Debugging Memory profiling + C-integration
Learn Go Have a couple of hours to spare? Learn Go! tour.golang.org
Learn Go Have a couple of hours to spare? Learn Go! tour.golang.org Download our thesis @ bit.ly/go-thesis