GoLang Tutorial: Variable Tutorial For Beginners

A variable is an identifier for allocating memory to store a specific value.

Variable Declaration

Variable declaration in GO has the following syntax.

An example variable declaration is shown below.

Try in Go Playground

In the above code we declared the variable type string, however, we have assigned any value to it. So go automatically assigns the defaults value as empty. If the type is int, the default value will be zero.

Now let’s try to assign some value to few variable types.

Go Type Inference

One good thing about go is, you don’t have to declare the variable type like we did before. Go will automatically infer the variable type. For example

Multiple Variable Declaration

A single variable declaration can contain multiple variables.

Multiple variables can be declared in the following way as well.

Try in Go Playground

In the above example, we have four variables. Two variables have a value assigned during initialization while other two got initialized first with the variable type and later the values for assigned.

Shorthand Declaration

You can use the shorthand operator (:=) for assigning variables. Using shorthand you an initialize and assign variables in single line.

 

Related

golang for devops

Create a file named main.go in your project directory. [crayon-5bf446ac92ad9589066539/] From the above code, you…

Read more

Internet is huge! Help us find great content

Newsletter

Never miss a thing! Sign up for our newsletter to stay updated.

About

The Best Tutorials & Tips to Speedup Your DevOps Workflow.

Created by Bibin Wilson.

All Udemy Course For $9.99