{"id":822,"date":"2009-11-11T16:08:33","date_gmt":"2009-11-11T16:08:33","guid":{"rendered":"http:\/\/scientopia.org\/blogs\/goodmath\/2009\/11\/11\/googles-new-language-go\/"},"modified":"2009-11-11T16:08:33","modified_gmt":"2009-11-11T16:08:33","slug":"googles-new-language-go","status":"publish","type":"post","link":"http:\/\/www.goodmath.org\/blog\/2009\/11\/11\/googles-new-language-go\/","title":{"rendered":"Google&#039;s New Language: Go"},"content":{"rendered":"<p> I&#8217;ve been being peppered with questions about <a href=\"http:\/\/golang.org\">Go<\/a>, the new programming language just released as open-source by Google. Yes, I know about it. And yes, I&#8217;ve used it. And yes, I&#8217;ve got some strong opinions about it.<\/p>\n<p> Go is an interesting language. I think that there are many fantastic things about it. I also think that there are some really dreadful things about it.<\/p>\n<p> A warning before I go on: this post is definitely a bit of a rush job. I wanted to get something out before my mailbox explodes :-). I&#8217;ll probably try to do a couple of more polished posts about Go later. But this should give you a first taste.<\/p>\n<p><!--more--><\/p>\n<p> The natural question is, what does it look like? It&#8217;s vaguely C-like, but with a lot of cleanups and simplifications. Every declaration is preceded by a keyword that identifies what it&#8217;s declaring: <code>type<\/code>, <code>func<\/code>, <code>var<\/code>, or <code>const<\/code>. Lots of parens that are required in C have been removed. The type declarations have been cleaned up quite a lot &#8211; they&#8217;ve gotten rid of the type-declaration garbage of C.<\/p>\n<p> In every declaration, the name comes first, followed by the type. So, for example, types are declared <em>after<\/em> variable names, and all type modifiers precede the types. So <code>*X<\/code> is a pointer to an <code>X<\/code>; <code>[3]X<\/code> is an array of three <code>X<\/code>&#8216;s. The types are therefore really easy to read just read out the names of the type modifiers: <code>[]<\/code> declares something called an array slice; &#8220;*&#8221; declares a pointer; <code>[size]<\/code> declares an array. So <code>[]*[3]*int<\/code> is an array slice of pointers to arrays of three pointers to ints.<\/p>\n<p> Functions in Go are amazing. They start off really simply, but by providing a few simple extensions, they let you do all sorts of things. To start off,  here&#8217;s a factorial function in Go.<\/p>\n<pre>\nfunc Factorial(x int) int {\n  if x == 0 {\n    return 1\n  } else {\n    return x * Factorial(x - 1)\n  }\n}\n<\/pre>\n<p> Go extends that by adding support for named return values. You can declare the return value as a variable in the function header; then you can assign values to that variable. When the function returns, the last value assigned to the return variable is the return value. So you could also write factorial as:<\/p>\n<pre>\nfunc Factorial(x int) (result int) {\n  if x == 0 {\n    result = 1\n  } else {\n    result = x * Factorial(x - 1)\n  }\n  return\n}\n<\/pre>\n<p> You can also write a function with multiple return values:<\/p>\n<pre>\nfunc fib(n) (val int, pos int) {\n  if n == 0 {\n    val = 1\n    pos = 0\n  } else if n == 1 {\n    val = 1\n    pos = 1\n  } else {\n    v1, _ := fib(n-1)\n    v2,_ := fib(n-2)\n    val = v1 + v2\n    pos = n\n  }\n  return\n}\n<\/pre>\n<p> It&#8217;s not object-oriented in a traditional way; it provides something like a limited form of object-orientation using another extension to functions. You can define new types &#8211; either type aliases, or structure types. You can declare methods for <em>any<\/em> type at all in the module where it&#8217;s defined. A method is just a function which has a parameter <em>preceeding<\/em> the function name.<\/p>\n<p> So, for example, a basic linked list could be implemented as;<\/p>\n<pre>\ntype IntList struct {\n  next *IntList\n  val int\n}\n\nfunc (s *IntList) SetNext(n *IntList) {\n  s.next = n\n}\n\nfunc (s *IntList) GetNext() *IntList {\n  return s.next\n}\n\nfunc (s *IntList) SetValue(v int) {\n  s.val = v\n}\n\nfunc (s *IntList) GetValue() int {\n  return s.val\n}\n<\/pre>\n<p> One of the dominant themes that you&#8217;ll see as I continue to describe it is minimalism. The guys who designed Go were very focused on keeping things as small and simple as possible. When you look at it in contrast to a language like C++, it&#8217;s absolutely striking. Go is very small, and very simple. There&#8217;s no cruft. No redundancy. Everything has been pared down. But for the most part, they give you what you need. If you want a C-like language with some basic object-oriented features and garbage collection, Go is about as simple as you could realistically hope to get.<\/p>\n<p> To give you one example of that minimalist approach, Go allows you to define types with methods. But there&#8217;s no such thing as a class, and there&#8217;s absolutely no inheritance. Instead, there&#8217;s a kind of composition. I won&#8217;t go into detail about it here. <\/p>\n<p> As I said above, you can define methods on <em>any<\/em> type. The methods are really just functions with a special parameter. There&#8217;s no such thing as a type constructor! There&#8217;s an allocation operator, &#8220;new&#8221; &#8211; but it doesn&#8217;t initialize values. You can&#8217;t provide Go with any automatic initializer. Instead, things like constructors are handled by convention. You&#8217;ll generally create a new data type inside of a module. That module will have a public function named &#8220;New&#8221;, which returns an initialized value. So, for example, there&#8217;s a module named &#8220;vector&#8221; containing an implementation of a vector; to create a vector, you import the vector module, and call <code>vector.New(size)<\/code>.<\/p>\n<p> Another example of that minimalism is the way that they handle abstraction and name hiding. There are exactly two kinds of name visibility: public, and private. Private things can only be seen in the module that declared them; public things can be seen in any module that imports them. The way that you make things public is by lexical cues: public things are things that weredeclared with an identifier starting with an upper case letter. &#8220;fact&#8221; is private, &#8220;Fact&#8221; is public. <\/p>\n<p> The most innovative thing about it is its type system. There are two kinds of types in Go: concrete types, and interface types. Concrete types are exactly what you&#8217;re used to from most programming languages. Interface types are similar to interface types in languages like Java, with one <em>huge<\/em> exception: you don&#8217;t need to declare what interface types you implement! An interface is a specification of what methods a type must provide to be used in some context. <em>Anything<\/em> which implements those methods implements the interface. Even if the interface was defined later than a type, in a different module, compiled separately, if the object implements the methods named in the interface, then it implements the interface.<\/p>\n<p> To make that even better, methods aren&#8217;t limited to objects. In fact, Go doesn&#8217;t really have objects. Any value, any type at all, can have methods. So you can make an integer type with its own methods. For example:<\/p>\n<pre>\ntype Foo int;\nfunc (self Foo) Emit() {\n  fmt.Printf(\"%v\", self)\n}\ntype Emitter interface {\n  Emit()\n}\n<\/pre>\n<p> <code>Foo<\/code> implements <code>Emitter<\/code>. I&#8217;ve created the type &#8220;Foo&#8221;. Values of type &#8220;Foo&#8221; are integers. Then I&#8217;ve implemented a method on an instance of &#8220;Foo&#8221;.<\/p>\n<p> That&#8217;s <em>brilliant<\/em>. I absolutely love it. And even in the relatively small amount of time I&#8217;ve spent hacking Go code, I&#8217;ve seen it pay off, when I&#8217;ve created new interfaces, and realized that old types already implement the interface. It&#8217;s a very elegant idea, and it works really well. It ends up giving you something with the flavor of Python-ish duck typing, but with full type-checking from the compiler.<\/p>\n<p> For example, the first thing I wrote in Go was a parser combinator library. I defined an interface for the input to a parser:<\/p>\n<pre>\n\/\/ ParserInput represents an input source readable by a\n\/\/ parser.\ntype ParserInput interface {\n  \/\/ Get the character at an arbitrary position in the\n  \/\/ input source.\n  CharAt(i int) uint8\n\/\/ Get the number of characters in the input source.\n  Size() int\n}\n<\/pre>\n<p> Now, anything that implements <code>CharAt(int)<\/code> and <code>Size()<\/code> can be used as an input to a parser. Then, I defined an interface for parsers:<\/p>\n<pre>\n\/\/ ParseValue represents the type of values returned by\n\/\/ successful parses.\ntype ParseValue interface { }\n\/\/ A Parser is an object which parses input sources. The\n\/\/ framework of parser combinators provides a very general,\n\/\/ backtracking parser.\ntype Parser interface {\n\/\/ Run the parser on an input source, starting with\n\/\/ the character at a specified position. If the parse\n\/\/ succeeds, it returns \"true\" as the status, the\n\/\/ number of characters matched by the parser as the match_len,\n\/\/ and an arbitrary parser-specified, return value as the result.\n\/\/ If the parse fails, then it returns false as the status, -1 as\n\/\/ match_len, and nil as the result.\n  Parse(in ParserInput, pos int)\n    (status bool, match_len int, result ParseValue)\n}\n<\/pre>\n<p> So a parser is anything which has a <code>Parse<\/code> method. A parse<br \/>\nmethod takes an input, and a position in the input; and it returns three<br \/>\nvalues: a success code (indicating whether the parse succeeded or failed); a match length (indicating how many characters in the input were accepted by the parser), and a return value. The return value can be <em>anything<\/em>: it&#8217;s defined as an empty interface, and <em>everything<\/em> implements the empty interface.<\/p>\n<p> To build parsers, you start with a kind of parser that can process a single input character, which is any character from some set of characters.<\/p>\n<pre>\n\/\/ CharSetParser parses a single character from among a\n\/\/ specified set.\ntype CharSetParser struct {\n  chars string\n}\nfunc (self *CharSetParser) Parse(in ParserInput, pos int)\n    (status bool, match_len int, result ParseValue) {\n  status = false\n  match_len = -1\n  for c := range self.chars {\n    if self.chars[c] == in.CharAt(pos) {\n      result = self.chars[c]\n      match_len = 1\n      status = true\n    }\n  }\n  return\n}\n<\/pre>\n<p> This demonstrates one sort of odd feature of Go. A structure and a pointer to a structure are <em>different types<\/em>, and they can have <em>different methods<\/em>. A <em>pointer to<\/em> a <code>CharSetParser<\/code> is a parser; but a <code>CharSetParser<\/code> is not!<\/p>\n<p> The implementation above doesn&#8217;t provide a way of creating a <code>CharSetParser<\/code>. To do that, we need a function. And to try to make the parsers look as clean as possible, we&#8217;ll name it so that it doesn&#8217;t look like a creator function, but just a parser itself:<\/p>\n<pre>\n\/\/ Create a CharSetParser which accepts any one character\n\/\/ from a specified string.\nfunc CharSet(s string) *CharSetParser {\n  return &amp;CharSetParser{ s }\n}\n<\/pre>\n<p> Then you add more kinds of parsers which allow you to combine simple parsers. For example, one combinator is repetition, which runs a parser over and over until it succeeds:<\/p>\n<pre>\n\/\/ ManyParser is a parser that parses a repeated syntax\n\/\/ element. It succeeds if the sub-parser succeeds at least\n\/\/ a specified minimum number of times. Returns a list\n\/\/ of the results of the sub-parses.\ntype ManyParser struct {\n  min int\n  parser Parser\n}\n\n\/\/ Create a ManyParser which matches min or more\n\/\/ repetitions of the sequence parsed by p.\nfunc Many(p Parser, min int) *ManyParser {\n  result := &amp;ManyParser{ min, p }\n  return result\n}\n\nfunc (self *ManyParser) Parse(in ParserInput, pos int)\n    (status bool, match_len int, results ParseValue) {\n  status = false\n  curPos := pos\n  numMatches := 0\n  stepResults := vector.New(0)\n  stepSuccess, stepLen, stepResult := self.parser.Parse(in, curPos)\n  for stepSuccess {\n    numMatches++\n    curPos = curPos + stepLen\n    stepResults.Push(stepResult)\n    stepSuccess, stepLen, stepResult = self.parser.Parse(in, curPos)\n  }\n  if numMatches &lt; self.min {\n    stepSuccess = false\n    match_len = -1\n    results = nil\n    return\n  }\n  status = true\n  results = stepResults\n  match_len = curPos - pos\n  return\n}\n<\/pre>\n<p> With that, you just say <code>Many(P, 0)<\/code> to represent a parser that parses 0 or more repetitions of P. <\/p>\n<p> Anyway, the end result is a library where you can write lots of very simple functions which combine parsers in very flexible ways. The end result is something where I can write a parser that parses lisp SExpressions into a cons-list structure with: <\/p>\n<pre>\nSexprParserRef := MakeRef()\nManySexprs := Action(Many(SexprParserRef, 1), new(VectAction))\nListParser := Second(Seq([]Parser { Lp, ManySexprs, Rp }))\nSexpr := Alt([]Parser{ ListParser, SymParser })\nSexprParserRef.SetTarget(Sexpr)\n<\/pre>\n<p> Alas, not everything about it is so wonderful. Sometimes that minimalism comes back to bite you on your ass. And worse, there&#8217;s a bit of a &#8220;for me but not for thee&#8221; attitude that pervades some aspects of the language design.<\/p>\n<p> For one example of an odd minimalist tradeoff: they decided that they didn&#8217;t want to specifically add something like an enumeration type. After all, what&#8217;s an enumeration? It&#8217;s really a type-safe alias for an integer, with a set of constants defining the members of the enumeration. Go already has the ability to define a type-safe alias for an integer! So why bother allowing the definition of a new kind of type? Instead, just make it easy to define the constants. So they created a pseudo-variable named <code>iota<\/code> which can be used inside of a constant declaration block. Each time you see a semicolon in a constant block, iota is automatically incremented. So to define a set of colors, you could do something like:<\/p>\n<pre>\ntype Color int\nconst (\n  RED Color = iota\n  ORANGE Color = iota\n  YELLOW  Color = iota\n  GREEN Color = iota\n  BLUE Color = iota\n  INDIGO Color = iota\n  VIOLET Color = iota\n)\n<\/pre>\n<p> That will create a color type,  with &#8220;RED&#8221; as 0, &#8220;ORANGE&#8221; as 1, etc. Of course, it&#8217;s kind of annoying to have to re-type the iota every time. So if you omit the values after an iota in a const block, it automatically just copies them. So you could rewrite the colors as:<\/p>\n<pre>\ntype Color int\nconst (\n  RED Color = iota\n  ORANGE\n  YELLOW\n  GREEN\n  BLUE\n  INDIGO\n  VIOLET\n)\n<\/pre>\n<p> It seems a bit strange and obtuse, but not awful. But it can get downright strange. Here&#8217;s an example from the Go tutorial:<\/p>\n<pre>\ntype ByteSize float64\nconst (\n  _ = iota;\t\/\/ ignore first value by assigning to blank identifier\n  KB ByteSize = 1&lt;&lt;(10*iota)\n  MB\n  GB\n  TB\n  PB\n  YB\n)\n<\/pre>\n<p> Iota starts at 0. By assigning it to &#8220;_&#8221;, you effectively discard the zero value. Then, &#8220;KB&#8221; is defined as 2<sup>10*iota<\/sup>. That full expression is then copied to the successive values. So MB gets a copy of <code>Bytesize = 1&lt;&lt;(10*iota)<\/code>, which evaluates to 1&lt;&lt;(10*2). And so on. Since <code>ByteSize<\/code> is an alias for a <code>float64<\/code>, the values are automatically converted to floats.<\/p>\n<p> It&#8217;s powerful, but if you ask me, it&#8217;s ad-hoc and ugly.<\/p>\n<p> Then there&#8217;s the &#8220;We&#8217;re the language designers, we need stuff you don&#8217;t&#8221;.<\/p>\n<p> The Go type system does <em>not<\/em> support generic types. They&#8217;re considering adding them at some point in the future, but for now, they don&#8217;t consider them necessary. Lowly programmers just don&#8217;t <em>need<\/em> parametrics, and it would clutter up the beautiful compiler to implement them.<\/p>\n<p> Oh, but wait&#8230; Go <em>really<\/em> needs type-safe arrays. Well, that&#8217;s OK. Everyone provides arrays as a sort of special case &#8211; what language doesn&#8217;t have typed arrays, even if it doesn&#8217;t have any other parametric types? And we really want these cool things called slices &#8211; but they really need to be strongly typed. So we&#8217;ll let <em>them<\/em> be parametric. And maps &#8211; we really need a map type, which maps keys to values, and it really needs to be type-safe. So we&#8217;ll add a parametric map type to the language, by making it a special case built-in.<\/p>\n<p> So: you can&#8217;t write parametric types &#8211; but <em>they<\/em> can. And that creates a very weird asymmetry to the language. Everything in Go is passed by value &#8211; <em>except<\/em> for the built-in slice and map types, which are passed by reference. Everything is allocated by &#8220;new&#8221; &#8211; <em>except<\/em> for the built-in slice and map types, which are allocated by &#8220;make&#8221;. It&#8217;s by far the biggest blemish in Go, and it&#8217;s absolutely infuriating.<\/p>\n<li> There&#8217;s a similar issue around error handling. The language doesn&#8217;t have exceptions. I can accept that &#8211; exception handling in most languages is absolutely hideous, and you can make a strong argument that it&#8217;s so damned broken that you should find a way to do without. Particularly in a language like Go, where you can return multiple values from functions, you can do things like return both a result and a status code, and handle errors via status codes.<\/p>\n<p> So the Go guys left out exceptions. They say that you don&#8217;t need them. But for their own code, they added workarounds that become something like exception handling for their own code. For example, if you want to do a cast from an interface type X to an interface type Y, you&#8217;d write something interface type to another, you&#8217;d write &#8220;<code>y = x.(Y)<\/code>&#8220;. But if the value x didn&#8217;t implement interface <code>Y<\/code>, it would be an error, and the program would crash. So if you&#8217;re not sure that x implements <code>Y<\/code>, you can write &#8220;<code>y, ok := x.(Y)<\/code>&#8220;. That never fails; if x implements <code>Y<\/code>, then y gets the casted value, and ok is true. If x doesn&#8217;t implement <code>Y<\/code>, then y get assigned nil, and ok is set to false. Once again, having a way of catching an error is OK for the language designers, but not for anyone else.<\/p>\n<p> Anyway, with the complaints out of the way: one other major piece of goodness is compilation speed. One of the fundamental goals of Go was to be able to compile things <em>really<\/em> quickly. Lots of things about the language were designed to make it possible to build a really fast compiler, and to be able to do full separate compilation without every needing to re-process anything. (The motivation for this is that at Google, we have a very large code-base with tons and tons of code re-use. This is a very good thing. But because most of that code is C++, builds can be incredibly slow. The way that C++ header files work with a standard compiler, you can wind up re-parsing the same file hundreds or thousands of times. So even with a really fast compiler, you can easily wind up with some extremely slow compile times. Tricks like pre-compiling headers can help, but they&#8217;ve got their own problems.)<\/p>\n<p> Go programs compile really astonishingly quickly. When I first tried it, I thought that I had made a mistake building the compiler. It was just too damned fast. I&#8217;d never seen anything quite like it. I&#8217;d taken a parser combinator library that I&#8217;d written in Java, and re-implemented it in Go &#8211; the full version of the code that I excerpted above. The code was slightly more than 30% shorter in Go, and also cleaner and prettier than the Java. A full, clean build of the combinator library in Java took just over 3 seconds in Eclipse. It took 0.6 seconds on the command-line compiled with Jikes. With the 6g go compiler, it took 0.06 seconds!<\/p>\n<p> Before using Go, Jikes was the fastest compiler I&#8217;d ever used. But Go managed to do better by a factor of 10! And Jikes was generating Java bytecode; 6g is generating reasonably well-optimized native code! Part of that is the genius of Ken Thompson, the guy who implemented 6g; but part of it is also the very careful design of the language.<\/p>\n<p> <em>(Boneheaded mistake 2: for some reason, I always confuse Ken Thompson and Dennis Ritchie. It&#8217;s Ken who wrote 6g, not Dennis, as I originally wrote.)<\/em><\/p>\n<p> So&#8230; At the end of the day, what do I think? I like Go, but I don&#8217;t love it. If it had generics, it would definitely be my favorite of the C\/C++\/C#\/Java family. It&#8217;s got a very elegant simplicity to it which I really like. The interface type system is wonderful. The overall structure of programs and modules is excellent. But it&#8217;s got some ugliness. Some of the ugliness is fixable, and some of it isn&#8217;t. On balance, I think it&#8217;s a really good language, but it could have been a lot better. It&#8217;s not going to wipe C++ off the face of the earth. But I think it will establish itself as a solid alternative. And hopefully, over time, they&#8217;ll fix some of the worst parts of the ugliness, without sacrificing the beauty or simplicity of the language. (Addendum 2011: I&#8217;ve used Go a whole lot since I wrote this&#8230; and at this point, I absolutely <em>love<\/em> Go. It&#8217;s become one of my favorite languages. It&#8217;s just so much <em>fun<\/em> to program in. Yeah, it&#8217;s got its blemishes &#8211; but it&#8217;s a delight to work with.)<\/p>\n<p> To preemptively answer a question I&#8217;m sure people will ask: am I using Go for my daily work? Not yet. My project has a lot of existing code, and at the moment, I&#8217;m constrained by that. I&#8217;ll be starting work on a prototype of a major new component very soon, and once I&#8217;ve had time to look at the foreign function interface for Go, I&#8217;ll make a decision about whether or not to use it. Basically, if the FFI is good enough to allow me to link to the Google infrastructure that I need to be able to use, then I will use Go for my prototype, and hopefully for the full component.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been being peppered with questions about Go, the new programming language just released as open-source by Google. Yes, I know about it. And yes, I&#8217;ve used it. And yes, I&#8217;ve got some strong opinions about it. Go is an interesting language. I think that there are many fantastic things about it. I also think [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[54],"tags":[],"class_list":["post-822","post","type-post","status-publish","format-standard","hentry","category-programming"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p4lzZS-dg","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts\/822","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/comments?post=822"}],"version-history":[{"count":0,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/posts\/822\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/media?parent=822"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/categories?post=822"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.goodmath.org\/blog\/wp-json\/wp\/v2\/tags?post=822"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}