Go Language for Beginners Part 2 - Declaring Variables in Go

Part 2 - Declaring Variables in Go

Hello!

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

In this post we're going to discuss about Declaring Variables in Go.

Variables in Go are explicitly declared and as we discussed in the previous post, Go variables work with types!

Declaring Types for the Variable in Go

In this first example we're going to declare the type of the variable:

The pattern do declare is:

Declaring Multiple Variables at Once in Go

We have the facility to declare multiple variables at once:

Declaring Multiples Variables at Once in Go and Initializing it

Go enables us to declare multiple variables (or single) and initialize those variables at the same time:

Inferring Types of Initialized Variables in Go

Go is a static typed programming language with the beauty of dynamic programming languages! You should have a variable with a type but Go is really smart and can infer this type for you!

Great! Notice that we omitted the type int in the variable initialization! :tada:

Shorthand for Declaring and Initializing a Variable in Go

Go has an awesome shorthand for:

  • declaring a variable
  • initializing this variable
  • inferring this type

All at the same time!!

The pattern of the syntax is:

So, let's code:

Shorthand for Declaring Multiple Variables in Go

As you can imagine, Go can have this shorthand for declaring multiple variables:

Go is awesome, isn't? : )

Assigning Different Value of Type into another Declared Variable

Just to remember from the last post, we have a few types in Go:

The output will be:

But what about trying to assign a value typed as a float into a value typed as an int?

If you run this code you'll have the following error:

So, we can't assign a type float into a type int. It sounds obvious because you can have a float number greater than the int number, meaning that you could lost data.

But what about trying to assign an int value into a float value? It sounds ok, since every int fits in float type:

Let's make a little change in the code above:

Now we're trying to assign the value of the number variable, that is an int, into the price variable, that is a float.

If you run this code you surprisingly will get the following error being thrown in your face:

Yes, even with a number that fits into the type, you can't assign it in Go!

It's time to fix it! :smirk:

Explicitly Casting Types in Go

In this code we're explicitly casting the number value into a float64:

This code executes successfully, since we're casting the int value into a float64 type by using the syntax:

Great!

That's it! In the next post: Part 3 - Functions in Go we're going to see how to work with Functions in Go!

Don't forget to explore all parts to the Go Series for Beginners in 16 Parts!!

Thanks!

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 Language for Beginners in 16 Parts! | Hacking Code - […] 2 - Declaring Variables in Go […]

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: