In 2018, I was hired by a company that uses Go. After 8+ years of working with Ruby, the first thing I did to learn Go is to relate to what I know in Ruby. And I thought it would be a good idea to document my learning process. My initial intention is keep it as my personal documentation and notes, but after much careful thought and considerations, I decided to post it online. From one topic, it grew into a series of multiple online articles, discussing all about my research and learnings about the language, collecting its analogies that I can compare with Ruby to help me learn it.
Go is a great language, its easy to read, with clear syntax. And it compiles to a single binary file which makes apps fast and compact, and can compile to run on different platforms. And its statically typed and garbage-collected making it efficient.
Its like a modern C with package support, memory safety, automatic garbage collection and concurrency baked-in. And you get all the nice features from a statically typed language, IDEs loves it, and so your development workflow.
In todays world of cloud native microservices, containerized architectures, You can be up-to-date with a knowledge in Go. Many notable open-source projects are built using Go (i.e. Docker, Kubernetes, Etherium and Terraform to name a few), and those platforms have APIs and SDKs readily available natively for you to use. And many global companies have been using Go in production (i.e. Google, Netflix, Dropbox, Heroku and Uber to name a few), proving that it has been battle-tested and powerful mature language to based your work into.
This book was made with a Rubyist in mind, all the learning methapors are based on Ruby and I think it will help you to learn Go programming language when you already knows Ruby. Go can be your second or third programming language and this book can help you get started.
Introduction
One of my favorite programming insights is in the book titled Class Constructions in C and C++: Object-Oriented Programming Fundamentals by Robert Sessions published in 1992, which reads object-oriented programming is really just a common-sense extension of structured programming. Fast forward today, this mindset still holds true not only for C programming language but also in Go. Go is not an OOP language by choice, and we can apply OOP techniques from OOP languages like Ruby to Go.
Go is an ideal programming language companies and developers choose to transition to, it is robust, fast, cross-platform, easy to learn, and a good choice for backend development.
It is also backed by Google, which ensures that it has supported developments in the long run.
The book is written in the way that it is easy and practical, and following through reading this book will help you to get up to speed on programming in Go fast.
And if you have prior Ruby experience, this book will help you learn Go faster, because the examples are written in Ruby, that you can relate easily.
About the Author
Joel has over 12+ years of experience as a software engineer in various IT domains namely Cybersecurity, OTT, Sports, PaAS, VoIP, Hospitality and E-commerce. He started programming when he was 12. He lives in Amsterdam, together with his family.
Setter/Getters, Attribute Accessors and Structs
Everything needs communication. Animals and humans communicate using physical gestures and sounds. Most insects have special antenna in them that they use to communicate to other insects, or emits a sounds or a scents that allows it to exchange information to other animals. Plants also have a special way to communicate to other plants through electric signalling or using others hosts to send their message.
From gigantic animals to small microscopic bacterias, viruses to cells, all have a special way to exchange message, they all have a way to coordinate information with each other.
Computers and software also requires a lot of communication.
Inside the computer hardware, electrical signals are sent to each individual components where they are task to perform a very specific function. And computers itself communicate to other computers via wire, wireless and The Internet.
It is hard to image a world without communication!
In programming, there are also various levels of ways to communicate to your code. In this chapter, we will discuss ways we can communicate to individual parts of code, one of them is variables.
If you need to pass, exchange and coordinate values that are bound to specific parts of the code, then we can use a variable.
Variables can either be a local variable that you can only access from inside the function, or global variable that you can globally access throughout your code.
For our first program example, we will create a function that takes a single word input, then output the word Hello World.
Here is an example of a local variable in Ruby. In this example, we declared a local variable hello
inside a function say_hello
.
1
def
say_hello
(
message
)
2
hello
=
"Hello"
3
puts
hello
+
message
4
end
5
6
say_hello
(
"World"
)
And when we run the Ruby code, it will output Hello World.
1
$
ruby say_hello.rb2
3
Hello World
However, if we want to change the hello
variable to store a different message, we cannot directly change it from outside the function.
1
def
say_hello
(
message
)
2
hello
=
"Hello"
3
puts
hello
+
message
4
end
5
6
hello
=
"Hi"
7
say_hello
(
"World"
)
So even though we change the hello
variable, this will still output a Hello World.
1
$
ruby say_hello.rb2
3
Hello World
Questions
1. What have we learned?
We learned that we can use a local variable to store a value that we can only access from inside the function.