DEV Community

Cover image for Go Basics: Variables
Abhinav Pandey
Abhinav Pandey

Posted on • Edited on

14 5 1 1

Go Basics: Variables

In the last article, I gave a small introduction to programming with Go, where we saw the tiniest possible programs and the general syntax.

Continuing on the syntax, let's take a look at some more syntax and understand how variables are declared in Go.

If you're completely new to Go, please check out the previous article:

Variables

Variables are declared using the var keyword and are used to store values.

The following is an example of a variable declaration.

var name string = "John"
Enter fullscreen mode Exit fullscreen mode

The variable name is of type string and is assigned the value John.

You can also declare multiple variables at once.

var (
    name string = "John"
    age int = 30
)
Enter fullscreen mode Exit fullscreen mode

You can also declare variables with a type but without an initial value.

var name string
Enter fullscreen mode Exit fullscreen mode

You can also declare multiple variables with a type but without an initial value.

var (
    name string
    age int
)
Enter fullscreen mode Exit fullscreen mode

Or multiple variables of the same type in a single line.

var name, occupation string
Enter fullscreen mode Exit fullscreen mode

Here, name and occupation are of type string.

Shorthand declaration of variables

You can also use the := operator to declare and assign a variable at the same time.

name := "John"
Enter fullscreen mode Exit fullscreen mode

Note that the := operator is only valid in short variable declarations. You cannot use it outside function bodies.
When using the := operator, the variable is declared with the type of the right-hand side expression. The var keyword is also not required.

Implicit type declaration
You can also declare a variable without specifying its type. In this case, the type is inferred from the initial value or a function call.
For example, the following code declares a variable age of type int and assigns the value 30 to it.

var age := 30
Enter fullscreen mode Exit fullscreen mode

Or using a function call.

var age := getAge()
Enter fullscreen mode Exit fullscreen mode

This also works for shorthand declarations.

Constants

Constants are declared using the const keyword. They are used to store values that cannot be changed.
For example, the following code declares a constant PI and assigns the value 3.14 to it.

const PI = 3.14
Enter fullscreen mode Exit fullscreen mode

Constants can also have implicit type declarations as in the above example.

  • They cannot be declared using the := operator.
  • They cannot be declared without an initial value.
  • They cannot accept a function call as an initial value.
  • The type of a constant may not be explicitly declared.

Thanks for reading.
This should give you an idea of how variables are declared in Go and how the syntax compares to other programming languages.

Stay tuned for more on Go.

If you want to connect with me, you can find me on Twitter @abh1navv.

Image of Datadog

Create and maintain end-to-end frontend tests

Learn best practices on creating frontend tests, testing on-premise apps, integrating tests into your CI/CD pipeline, and using Datadog’s testing tunnel.

Download The Guide

Top comments (1)

Collapse
 
arxeiss profile image
Pavel Kutáč

You forgot you can do multiple assignment a, b := "Hello", 22 and sure that can be with function returning multiple values.

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay