Getting Started with Go - Variables 
Tags: go, beginners, webdev, softwaredevelopment
Published: 2017-11-19 00:27:46

---
title: Getting Started with Go - Variables 
published: true
description: This is the second article in our Golang tutorial series and it deals with variables in Go. 
tags: go,beginners,webdev,softwaredevelopment
---

In the previous article - [Golang - Getting Started](https://dev.to/codehakase/golang---getting-started-16c), I wrote an intro to the Go programing language, and how to setup a development environment for your platform. In this article, we're going to talk about variables in Go.

## What is a Variable?
A Variable, is the name given to a memory location to store a value of a specific type. In Go, there are various syntaxes to declare variables:

### Single variable declaration 
```go
package main

import "fmt"

func main() {
  var bsLine string = "Man's not hot!"

  fmt.Println(bsLine)
}
```

`var bsLine string` declares a variable named `bsLine` of type `string`. We have not assigned any value for the variable. We assigned that variable a value of a string *Man's not hot!*. If there isn't any value assigned to a variable declaration, go automatically initializes it with the zero value of the variable's type. If you run this program, you'd see the following output:

```bash
$ cd $HOME/go/getting-started

$ go run variables.go

Man's not hot!
```

### Variable Type Inference
If a variable has an initial value, Go will automatically be able to infer the type of that variable using that initial value. Hence if a variable has an initial value, the type in the variable declaration can be omitted.

```go
package main 

import "fmt"

func main() {
  
  var tip = 300 // type inference happening here!
 
  fmt.Println("Let's tip him $", tip) // Let's tip him $300
}
```

### Multiple Variable Assignment 
In Go, you can declare or assign values to multiple variables in a single statement:

```go
package main

import "fmt"

func main() {
  
  var day, month, year int = 19, 11, 2017
  
  fmt.Printf("This article was posted on %d-%d-%d", day, month, year) // This article was posted on 19-11-2017
}
```

### Multiple assignments with different types, is that possible?
Yes it is. There's a different syntax for achieving this:

```go

package main

import "fmt"

func main() {
  
  var (
    name string = "Francis Sunday"
    alias string = "codehakase" 
  )

  fmt.Printf("Hi I'm %s, in the tech world I'm referred to as %s", name, alias)
}
```
The above code will print `Hi I'm Francis, in the tech world I'm referred to as codehakase`.

### Any shorthands?
Yes, there's a shorter way to declare variables, by using the special assignment operator `:=`

```go
package main

import "fmt"

func main() {
 
  publishDate := "19/11/2017"
  
  fmt.Println("Date: ", publishDate) // Date: 19/11/2017
}

```


## *Gotchas*

### Declaring non-new variables

```go

package main

import "fmt"

func main() {

  name, gender := "Francis", "male"
  
  fmt.Printf("Name: %d, Gender: %d", name, gender)
  
  gender := "female"
  
  fmt.Println("New gender: ", gender) //
}

```

The above code, declares and assigns values to two variables `name` and `gender`, and prints a formatted string below it. This program would **panic** immediately after the first print statement. it will throw error `no new variables on left side of :=`, because both the variables have already been declared and there are no new variables in the left side of `:=`. So if we needed to change the value of `gender`, we'd use the equal to operator `=` instead of `:=`.

```go
...

gender = "female"
```


## Conclusion
Hey! you've done a great job reaching this far, hopefully, you now understand how variables are defined in Go, and how to write simple programs that store data in variables. In the next article in the series, I'll be writing about **Types** in Go, so stay tuned. 

*If you missed the previous article, go check it out [HERE](https://dev.to/codehakase/golang---getting-started-16c).*

I'm certain I may have missed something, do me a favour and report this dude in the comments section below XD. 

*You can checkout my [Technical Blog](https://hakaselabs.github.io), and follow me on Twitter [@codehakase](https://twitter.com/codehakase)*