Go Language for Beginners in 16 Parts!

Go Language for Beginners in 16 Parts

Hello!

This is the fist post from the Golang Series: Go Language for Beginners in 16 Parts!

Go Language is one of the most important, exciting and easy Programming Language to learn!

Its syntax is concise, really clean and follows many patterns that keep us away from fighting for pointless things, like indentation, error handler, concurrency and so on.

In this Golang Series for Beginners we're going to see 16 Parts:

Before jumping crazy into a lot of hacked code, let's understand a few concepts about Go Lang!

Go Characteristics

Go was created by Google in 2009. Why?

  • Famous engineers at Google was tired to deal with complex Programming Languages like C++

How it was resolved then?

Go was designed to be really simple to read.

It brings the readability of Dynamic Programming Languages

Go was designed to be really simple to write.

It doesn't have magical and boilerplate code and has limited structural typing

Go was designed to be really fast

Go is written with C Language under the hood!

Go is Compiled

You don't need to have an Interpreter or a Virtual Machine and you don't need to make any changes to have your Go code be running in such environments

Go is a Statically Typed Programming Language

Even as a statically typed language, Go has a clean syntax and a great type inference, closed to a Dynamic Language

Go has a Garbage Collection

Great! You don't need to remember when to use malloc to allocate memory : )

Polymorphism in Go is great!

Go follows a different approach to deal with Polymorphism, brings to us a concept of Interface that is really simple, just being a collection of methods.

Go supports Networking and Multiprocessing in a beautiful way

Go was designed to have a built-in Concurrency approach with really lightweight processes with Goroutines and Channels

Hello, Go!

This will be the first Go code! We should create a file called go-hello.go

Let's compile the code

After that, a file will be created in the current directory named go-hello. This file is the compiled code that can be executed, as you can see below:

This 2 step approach is a little bit boring and Go can do it in just 1 step, using the go run command:

So, you don't want to try Go Language in your own machine? Don't worry, we can play in another place 🙂

Running Go Code in the Go Playground

I can't talk about Golang without mentioning its great Playground

Go there and run this same code by just clicking in the Run link!

Awesome, isn't it?

Packages and Functions in Go

Did you notice that we've called the function Println. This function comes from the package fmt

This package is a built-in package, given by the Golang by default. To use it, we just need to use the import keyword with the name of the package

Let's call a function from another package, in this example, from the package math:

Run this code on Go Playground or in you local terminal:

The output should be:

Calling Functions in Go

We can call another function in the same file:

If you run this code you'll get the following result:

Using Third-Party Packages in Golang

Go has a simple way to get an online package from GitHub. Just follow the pattern:

What about printing messages with Emojis in Go? Well, this is not a required and important feature (I think it is :P) in the Golang core, so let's use a package from GitHub

Following the pattern:

Now it's time to print Emojis in Go:

The output will print out emojis!

Go is a Statically Typed Language

This means that Go know which type your values are! String and Integer are both different from the Go perspective!

What about printing a String with an Integer? Let's create a file called types-in-go.go with the following code:

If you run the code:

You will have the following error message:

This error occurs at compile time. Try to just compile the code:

The same error will occur! Don't worry, we'll fix this code later : )

Showing the type of a value with TypeOf function

What about printing out the type of a value? Let's do that by using the TypeOf() method from the built-int reflect package:

If you run the code the result will be:

Great! In this simple post we got a little bit inside to the Go Language World! We have much work to do in 16 Parts! : )

In the next post: Part 2 - Declaring Variables in Go we're going to see how to use Type Declaration in Go!

That's it! I hope that was useful to you! Thank you 🙂

Follow us to keep up to date! \o/

Alexandre Gama

Alexandre Gama

Hacking Code Founder

Hacking Code Founder and Writer, Passionate Senior Software Engineer and Team Leader at @Elo7, Teacher of several courses at Caelum, Speaker at many conferences and terrible guitar player.
Go Language Tutorial for Beginners - Go Lang Characteristics - Hacking Code

Part 1 - Go Language Characteristics

This is the first Post of the complete series about Go Language!

Go Language Tutorial for Beginners - Declaring Variables in Go - Hacking Code

Part 2 - Declaring Variables in Go

How can we declare and use variables in Go? Check it out!

Go Language Tutorial for Beginners - Functions in Go - Hacking Code

Part 3 - Functions in Go

Do you want to try a more Functional Programming Style with Functions?

Go Language Tutorial for Beginners - Error Handling in Go - Hacking Code

Part 4 - Error Handling in Go

Errors always happen! How can we handle Errors in Go Language?

Go Language Tutorial for Beginners - Flow Control in Go - Hacking Code

Part 5 - Flow Control in Go

How to live without a Flow Control in Go? We can’t! Let’s see how to use it!

Go Language Tutorial for Beginners - Conditionals in Go - Hacking Code

Part 6 - Conditionals in Go

In this post we’re going to see how to work with Conditionals in Go!

Go Language Tutorial for Beginners - Switch in Go - Hacking Code

Part 7 - Switch in Go

Do you like to have many if statement in you code? So, let’s see switch statement in Go!

Go Language Tutorial for Beginners - Arrays in Go - Hacking Code

Part 8 - Arrays in Go

Data Structures in Go are really common! In this post we’re going to see Arrays in Go!

Go Language Tutorial for Beginners - Slices in Go - Hacking Code

Part 9 - Slices in Go

Slices in Go are really important and used in every code! Let’s learn about it!

Go Language Tutorial for Beginners - Maps in Go - Hacking Code

Part 10 - Maps in Go

What about using Maps in Go?

Go Language Tutorial for Beginners - Custom Types in Go - Hacking Code

Part 11 - Custom Types in Go

Let’s learn how to create and user Custom Types in Go

Go Language Tutorial for Beginners - Structs in Go - Hacking Code

Part 12 - Structs in Go

Structs in Go allow us to have types of an abstraction together! Let’s see how!

Go Language Tutorial for Beginners - Methods in Go - Hacking Code

Part 13 - Methods in Go

Methods in Go are created a little bit different from other Languages. Let’s see how?

Go Language Tutorial for Beginners - Interfaces in Go - Hacking Code

Part 14 - Interfaces in Go

Interfaces in Go are really different but exciting feature! Let’s see why!

Go Language Tutorial for Beginners - Concurrency with Goroutines in Go - Hacking Code

Part 15 - Concurrency with Goroutines in Go

Concurrency in Go is fantastic and simple! We’re going to see Goroutines now!

Go Language Tutorial for Beginners - Concurrency with Channels in Go - Hacking Code

Part 16 - Concurrency with Channels in Go

We can’t live with Goroutines without Channels when working with Concurrency in Go! Let’s see why?

0 Comments

Trackbacks/Pingbacks

  1. Go for Beginners Part 6 – Conditionals in Go | Hacking Code - […] 1 - Golang characteristics and Go Syntax […]

Leave a Reply

Share This
Subscribe To Our Newsletter

Subscribe To Our Newsletter

Join our mailing list to receive the latest news and updates from our team.

You have Successfully Subscribed!

%d bloggers like this: