Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Go for Beginners Part 2 - Declaring Variables in Go

DZone's Guide to

Go for Beginners Part 2 - Declaring Variables in Go

Welcome back to this series on Go! In this portion of the series, we explore of Go developers can declare variables in their Go code.

· Web Dev Zone ·
Free Resource

Deploying code to production can be filled with uncertainty. Reduce the risks, and deploy earlier and more often. Download this free guide to learn more. Brought to you in partnership with Rollbar.

Image title


Hello!

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

In this post, we're going to discuss 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 Variables in Go

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

package main

import "fmt"

func main() {
  var number int
  number = 10

  fmt.Println(number)
}

The pattern to declare is:

var yourVariable type
yourVariable = value

Declaring Multiple Variables at Once in Go

We have the facility to declare multiple variables at once:

package main

import "fmt"

func main() {
  var number, amount int
  number = 10
  amount = 15

  fmt.Println(number, amount)
}

Declaring Multiples Variables at Once in Go and Initializing Them

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

package main

import "fmt"

func main() {
  var number, amount = 10, 15
  var sum = 20

  fmt.Println(number, amount, sum)
}

Inferring Types of Initialized Variables in Go

Go is a static, typed programming language with all 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!

package main

import "fmt"

func main() {
  var number, amount = 10, 15
  var sum = 20

  fmt.Println(number, amount, sum)
}

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

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:

variable := value

So, let's code:

package main

import "fmt"

func main() {
  number := 10
  amount := 15

  fmt.Println(number, amount)
}

Shorthand for Declaring Multiple Variables in Go

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

package main

import "fmt"

func main() {
  number, amount := 10, 15

  fmt.Println(number, amount)
}

Go is awesome, isn't it?

Assigning Different Values of Type Into Another Declared Variable

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

package main

import (
  "fmt"
  "reflect"
)

func main() {
  fmt.Println(reflect.TypeOf(1))
  fmt.Println(reflect.TypeOf(9.5))
  fmt.Println(reflect.TypeOf("Just a String"))
  fmt.Println(reflect.TypeOf(true))
}

The output will be:

int
float64
string
bool

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

package main

import "fmt"

func main() {
  var number int = 10
  var price float64 = 15.10

  fmt.Println(number, price)

  number = price
  fmt.Println(number)
}

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

./7-assigning-different-type.go:11: cannot use price (type float64) as type int in assignment

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 lose 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 to the code above:

 price = number
 fmt.Println(price)

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:

cannot use number (type int) as type float64 in assignment

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:

package main

import "fmt"

func main() {
  var price float64 = 15

  fmt.Println("Price before:", price)

  var number int = 10

  price = float64(number)

  fmt.Println("Price after: ", price)
}

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

variable = float64(anotherVariable)

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 of the Go Series for Beginners in 16 Parts!

Thanks!

Deploying code to production can be filled with uncertainty. Reduce the risks, and deploy earlier and more often. Download this free guide to learn more. Brought to you in partnership with Rollbar.

Topics:
go ,golang ,variables ,web dev ,web application development

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}