This is the beginning of a blog series I am going to be releasing on a weekly basis. I’ll be writing about my experiences and findings as I learn to use the Go programming language. I am learning using a handful of forms of media: video, books, and lectures. For this week, I will be discussing what I have learned having completed ~ 25 video segments of Todd McLeod’s course.

Why Go?

  • Created by and used heavily at Google
  • I have heard it is very efficient
  • It is fairly in demand in the tech industry
  • It is used by a lot of companies
  • The community remains innovative and engaged

History

Rob Pike — Member of the core Unix team and created the UTF-8 coding scheme

Robert Griesemer — Worked closely with Niklaus Wirth, the creator of the programming language, Pascal

Ken Thompson — Designed and implemented the original Unix system as well as helped invent the C programming language

It is compiled, concurrent, garbage-collected, and statically typed.

Why should you care?

  • Efficient compilation
  • Efficient execution
  • Ease of programming

Short and sweet. Efficiency paired with ease? Good deal.

In the first week of learning Go I can already speak to how enjoyable it has been. I am so excited to dive into more.

What can you use Go for?

  • Command-line tools
  • Cryptography
  • Image Processing
  • Http / UDP / TCP
  • Concurrency (excited to learn more about this one!)

Type is king

Thinking idiomatically

id-i-om

a group of words established by usage as having a meaning not deducible from those of individual words

Essentially, “Idiomatic Go” is an agreed upon style and method of writing in the language that is blessed by the creators.

Smaller things to build bigger things

One of the first pieces of Go I was exposed to was the traditional “Hello world” program. I know at times it is easy to deem these examples “overly trivial” but, I find them helpful. You have to walk before you run, right?

package mainimport (    “fmt”)func main() {    fmt.Println(“Hello world”)}

Pretty simple right? Let me walk you through what is going on. First and foremost, every Go application must have a main package. These are commonly found in the main.go file in the ROOT of your application. A lot packages, including fmt, are from what is called the Standard Library of Go, they are lightweight and fast.

The entry point for all Go applications is the main function. This is where all of your code for your application is run. When this function is done executing code, your program is finished.

Thinking in terms of packages

Small, concise, and bite-sized.

Variables

Another cool feature of variables is the ability to “throw away” return values. This can be done like this:

package mainimport (    “fmt”)func main() {    id, _ := ReturnId()}

Inside of the main function I am expected to receive 2 return values from the ReturnId function; however, in this case I only care about the value of id. By using _ I am “throwing away” that 2nd return value.

Short declaration operator

:=← — short declaration operator (looks like a gopher)

This method of declaring variables seems to be the most common when writing Go programs. I believe a primary reason is due to it being at a function scope. Keeping your variables as close to the work you are performing on them reduces risk of side effects or changes to your variable.

  • has to be declared inside of a function body
  • can not be declared with a type

the var keyword

  • can be declared outside of a function body
  • can be declared with a type (int , string, map, etc)

Just to reiterate: it is best practice to use the short declaration operator whenever possible.

Types

  • Primitive data types: booleans, numeric (float, int, etc), and strings
  • Composite data types: array, struct, pointer, function, interface, slice, map, and channels

Zero value

var y string// “”var z int// 0var z bool// false// nil for pointers, functions, interfaces,// slices, channels, maps

Creating your own types

package mainimport (    “fmt”)var a int = 42type midichlorian intvar b midichlorianfunc main() {    fmt.Print.ln(a)    // 42    fmt.Printf(“%T\n”, a)    // int    b = 43    fmt.Print.ln(b)    // 43    fmt.Printf(“%T\n”, b)    // main.midichlorian}

As you can see above, I declare a variable with the name a of the type int. Next, I use the type keyword to define a new int type with the name midichlorian. Something to note here is that although I declared a new type called midichlorian, it’s underlying type is int.

“Convert” not “Cast”

package mainimport (    “fmt”)var a int = 42type midichlorian intvar b midichlorianfunc main() {    a = 42    fmt.Println(a)    // 42    fmt.Printf(“%T\n”, a)    // int    b = 43    fmt.Println(b)    // 43    fmt.Printf(“%T\n”, b)    // main.midichlorian    // CONVERSION <<<<<<    a = int(b)    fmt.Println(a)    // 43    fmt.Printf(“%T\n”, a)    // int}

As you see, we declare the variable b with the value of 43 with the type midichlorian. We then reassign the variable a with the return value of the int() conversion function on the b variable. Pretty cool, huh? With Go there is no uncertainty with the types you are working with.

In summary