I recently started attending a local Go meetup (virtually for the time). It has been quite an eye-opener learning a totally new language and not just another framework. Plus, Go is a statically typed language, quite different from the dynamically typed languages I have experience with such as Ruby and JavaScript. It's been quite a fun language to play with so far, and maybe one day I'll confidently refer to myself as an expert gopher. At any rate, I want to document my journey, so here is the first in a series of blog posts on Go.
The Origin of Go
You can read more about the origin of Go, along with other topics related to the language, at golang.org/doc/faq. To summarize, some developers at Google were frustrated with how complicated their code was using languages like C++ and Java. They wanted to create a new language that would be simpler to use. Simplicity is a key component of Go. One of the creators credits simplicity as the reason behind the language's success. This simplicity applies to some very complicated aspects of programming including concurrency, but we'll get into that later.
Why Go?
Reading more into the origin story can also answer the question of why someone should use Go in a project. Go is simple to use and fast. There are stories of people using Ruby on Rails and having scaling issues, like iron.io going from 30 servers to 2 by rewriting their API in Go. In the blog post, Dylan Stamat talks about having fun writing Ruby, not wanting to write more Java, and ultimately deciding on Go. Like Ruby, a lot of developers enjoy writing Go. That, combined with the fact Go is super easy to deploy (compiling a single binary) makes Go a great choice for many tasks!
Playing with Go
Now that we have some background let's dive into the language! If you want to install Go on your machine here is a link with all the instructions golang.org/doc/install, but if you don't want to for whatever reason, you can code in the cloud on play.golang! Quick tip: if you write some code in the go playground and hit share, your code will be saved to that url and you can revisit it or share it with others. Ex. a little code for the readers
Hello Devs
If you didn't have the courage to click on the last link, fear not, I will include the code here. Every great tutorial should include a print statement with the phrase "Hello, world." So here it is in Go.
package main
import "fmt"
func main() {
fmt.Println("Hello, devs!")
}
Although simple, this simple program touches on some key concepts of Go.
package main
We are declaring our package with the name main to let the Go compiler know that we want this package to compile to a binary executable instead of a shared library.
import "fmt"
Here we are importing the fmt (format) package which is part of the standard library. This is how easy it is to include another library! Important: Go does not like unused code. If you import a package and don't use it, the compiler will give you an error message.
func main() {
This is pretty standard stuff if you come from a C family language. Our program begins to execute in the main function. Important: Go follows the "one true brace style". This means an opening curly brace at the end of a line and not on a new line. Thanks to this strict policy, we don't have to tire our pinkies typing semicolons.
// good
func main() {
}
// unacceptable
func main()
{
}
fmt.Println("Hello, devs!")
Here we are accessing the fmt package and calling its function Println using OOP's familiar dot notation. Important Go is not an OOP language
Conclusion
We didn't go over too much, but now you are familiar with three of Go's 25 reserved keywords: package
, import
, and func
. Next week we'll go over variables and types.
Top comments (0)