JAOOThe Go Programming Language
Currently sitting in the Go language keynote in JAOO.
Go was created to handle Google needs:
- Compilation speed
- Efficient
- Fit for the current set of challenges we face
Go is meant to be Simple, Orthogonal, Succinct. Use C syntax, but leave a lot of complexity behind.
This make sense, because C/C++ is horrible for a lot of reasons, because of complexity added to it over the years. Actually, the major problem is that C/C++ mindset are literally designed for a different era. Take into account how long it can take to build a C++ application (I have seen no too big apps that had half an hour build times!), and you can figure out why Google wanted to have a better model.
Nice quotes:
- Programming shouldn’t be a game of Simon’s Says
- Difference between seat belts & training wheels – I am going to use that a lot.
I find Go’s types annoying:
- [variable name] [variable type] vs. [variable type] [variable name]
- And []int vs. int[]
I like the fact that is uses garage collection in native code, especially since if you reference a variable (including local) it will live as long as it has a reference to. Which is a common mistake in C.
It also have the notion of deferring code. This is similar to how you can use RAII in C++, but much more explicit, which is good, because RAII is always tricky. It very cleanly deals with the need to dispose things.
All methods looks like extension methods. Data is held in structures, not in classes. There is support for embedding structures inside one another, but Go isn’t an OO language.
Interfaces work for all types, and interfaces are satisfied implicitly! Much easier to work, think about this like super dynamic interfaces, but strong typed. Implies that we can retrofit things afterward.
Error handling is important, and one of the major reasons that I moved from C++ to .NET. Go have two modes, the first is error codes, which they use for most errors. This is especially nice since Go have multiple return values, so it is very simple to use. But it also have the notion of panics, which are similar to exceptions. Let us take a look at the following test code:
package main
func test() {
panic("ayende")
}
func main() {
test()
}
Which generate the following panic:
panic: ayende panic PC=0x2ab34dd47040 runtime.panic+0xb2 /sandbox/go/src/pkg/runtime/proc.c:1020 runtime.panic(0x2ab300000000, 0x2ab34dd470a0) main.test+0x47 /tmp/gosandbox-a9aaff6c_68e6b411_26fb5255_aa397bb1_6299a954/prog.go:5 main.test() main.main+0x18 /tmp/gosandbox-a9aaff6c_68e6b411_26fb5255_aa397bb1_6299a954/prog.go:8 main.main() mainstart+0xf /sandbox/go/src/pkg/runtime/amd64/asm.s:78 mainstart() goexit /sandbox/go/src/pkg/runtime/proc.c:145 goexit()
I got my stack trace, so everything is good. Error handling is more tricky. There isn’t a notion of try/catch, because panics aren’t really exceptions, but you can recover from panics:
package main import ( "fmt" ) func badCall() { panic("ayende") } func test() { defer func() { if e := recover(); e != nil { fmt.Printf("Panicing %s\r\n", e); } }() badCall() fmt.Printf("After bad call\r\n"); } func main() { fmt.Printf("Calling test\r\n"); test() fmt.Printf("Test completed\r\n"); }
Which will result in the following output:
Calling test Panicing ayende Test completed
All in all, if I need to write native code, I’ll probably go with Go instead of C now. Especially since the ideas that it have about multi threading are so compelling. I just wish that the windows port would be completed soon.
More posts in "JAOO" series:
- (06 Oct 2010) The Go Programming Language
- (20 Oct 2009) More on Evolving the Key/Value Programming Model to a Higher Level from Billy Newport
- (07 Oct 2009) OR/M += 2
- (05 Oct 2009) Evolving the Key/Value Programming Model to a Higher Level
- (05 Oct 2009) Working Effectively with Legacy Code 2 – Michael Feathers
Comments
I would still go with C++ because you will not find much Go tooling and libs. I started programming in Visual C++ so I know to a certain extent how painful C can be. Still, there are many patterns to help you out.
I have also spent some time playing with Go unfortunately the lack of proper exception handling and generics is a deal breaker for me - string handling is also not so great.
I really like their non-intrusive interface/type system - I hope C# will consider adopting something similar in future versions. The built-in CSP style concurrency also shows a lot of promise. However a lot of the areas where I thought about using go, I can also use node.js for - which is my current preference.
I also agree that it would be my preferred native language, however the next language on my tech radar is Scala on the JVM.
The syntax looks ugly for me, and after being shown D by students, I think I will consider D for native code, instead of C++. However I am not sure abut its parallel capabilities.
typo: garage collection.
'An array of ints' transformed into '[]int'? Nice :)
@Ayende
Checkout D programing language from digital mars, It has a more familiar feel especially coming from c#
I agree with a lot of your points. I don't like that weird way to type stuff, and I especially hate the exception handling. Overall it looks like a very nice language though.
The idea of using 'panic' for error handling looks very strange. Traditionally panic is an unrecoverable disaster when the program needs to terminate
Statically typed languages are a pain in the ass, dynamic is the future.
Google are really big on Go. They have versions for their flagship Android platform and their cloud App Engine. In addition there are many details including technical white papers on how Google is using Go internally for most of their native processing. Third party companies are actively investing in tools and IDE support. And the most popular platform Windows was supported very early on. The community is also thriving. Checkout the IT section on Amazon to, Go has the top 3 of the top 100 IT books.
Comment preview