DEV Community

Mfonobong Umondia (Bella)
Mfonobong Umondia (Bella)

Posted on

Declaring Variables in Golang

#go

In Golang (Go) programming, variables are the fundamental building blocks for data storage and manipulation. They are containers that hold the values that your code can access and modify, serving as the driving force of any program's functionality. Mastering variable declaration techniques is paramount for writing clean, maintainable, and efficient Go code.

This article provides a deep dive into declaring variables in Golang. It goes beyond a basic explanation and offers technical details for beginner Golang Developers who want to understand the in and out of variable declaration using the var and := keywords. It also covers zero values and best practices for choosing the right approach in different scenarios.

The var Keyword:

Before any variable is declared in Golang, you have to get used to the var keyword. This keyword is the primary way to declare variables. It allows you to specify the name, data type, and optionally, an initial value for the variable. Here's a breakdown of its functionality

var variableName type = value
e.g var name string = "Lady Bella"

Let's breakdown this declaration:

variableName: This represents the chosen identifier for your variable within your code just as used in the example "name". When writing this, ensure to select a descriptive name that adheres to Golang's naming conventions (lowercase with underscores for separation).

type: This element dictates the data type the variable will encompass. Golang has a rich set of built-in data types such as int (integers), string (text), bool (booleans), float64 (decimal numbers), and more.

value: This is used to assign an initial value during declaration. E.g "Bella". If this element is left vacant, Golang assigns the zero value specific to the data type (e.g., 0 for int, empty string for string).

Here's a practical example showcasing the declaration of multiple variables using var in Go:

var name string = "Lady Bella"
var age int = 26
var isEnrolled bool = true

Important things to note:

You can declare numerous variables of the same type on a single line, separated by commas.

If you can tell what kind of value a variable has from what you give it, you don't need to say the type. For example:

var name = "Lady Bella" // type inferred as string

The :=Shorthand:

Another way of declaring variables In Golang using shorthand. The := (colon equals) is a shorthand notation for declaring and initialising variables in Go. It's a concise way to define a variable and assign a value to it at the same time.

It usually starts with a variable name s then the :=. When using the shorthand variable declaration, it means that there is no need to use the var keyword to declare a variable datatype. It means that the value of this operator should be assigned to the variable. Here's a breakdown of its key aspects:

variableName := value
name := "Lady Bella"

This approach proves particularly useful when the data type of the variable can be inferred from the assigned value.

Example:

func greet(name string) {
message := "Welcome, " + name + "!"
fmt.Println(message)
}

Things to consider:

The := shorthand is restricted to function scopes; it cannot be used outside of functions.

It cannot be employed for redeclaration of existing variables within the same scope.

You should note that both var and := have their designated use cases:

Use var when you need to declare the type or intend to assign a value later.

Use := within functions for a concise declaration with type inference.

Always strive for readability and consistency in your codebase when making this decision.

Incorrect Values:

The datatype of a variable is very important because it defines what values should be assigned to it. For example, a variable with a type of string cannot be assigned an Integer. If assigned, the compiler will throw an error.

Let's look at an example below:

package main
import "fmt"

var s string
s = 123
func main() {
fmt.Println(s)
}

Also, when you declare a variable with var but leave the initial value unassigned, Golang will automatically assign the zero value specific to its data type. Here are some common zero values:

int: 0

float64: 0.0

string: "" (empty string)

bool: false

Understanding zero values is crucial for initialising variables before their usage to prevent unexpected behavior.

When you learn how to declare variables in Go, you can make strong and flexible programs. Remember to choose the right way (var or :=) based on what you need and always keep your code clear and consistent.

That will be all for this article. So far, we have covered the two ways of declaring variables in Go, choosing the right approach based on your needs, and maintaining code readability.

PS: This is officially my first technical article, and I would love to hear your feedback. Did I cover this topic well? Is it easy enough for beginners to follow? Your honest feedback in the comments will help improve my future articles.

Top comments (0)