A Guide to Variable and Their Declarations in Golang

Today we will be discussing how to declare and use variables in your Golang program. Let’s start by discussing the significance of variables in any programming language.
A variable can be defined as a temporary storage location in the main memory that holds data that your programs will be using. Remember, the program that you’re creating is a sequence of instructions that tells a computer to perform a particular operation(s). Such a program must use data—which can either be a constants, a fixed value or a variable value— while being executed.
The ability to understand how data management occurs in any programming language—Golang included—will set you apart from other programmers. This is because you’ll be able to write effective and efficient programs if you understand how data should be managed in a program.
So, how are variables used in Golang?
The Golang variables are usually given names—which act as identifiers—just like in any other programming language. Naming your variable appropriately is an important component of any programming language. In Golang, your variable name should always begin with a letter or an underscore. It can contain letters, numbers or even the underscore character.
However, your variable name can’t start with a number. At the same time, you can’t use special symbols when naming your variables. Also, ensure that the maximum characters for your variable is 255. We use the “var” statement to declare a list of variables in Golang. It doesn’t matter where you place your “var” statement.
It can be at the package level or the function level of your Golang code. For instance, the Golang code below illustrates this:

package main
import "fmt"
var sam, janet, ismarried bool
func main ()
var i int
fmt.Println (i, sam, janet, ismarried)

Your variable declaration can include initializers, in which case you can declare one per variable. If an initializer is present, then its type can be left out. For instance, the Golang code below illustrates this:

package main
import "fmt"
var sam, janet, ismarried bool=true
func main () {
var i , j int=1, 2
fmt.Println (i, j, sam, janet, ismarried)

If you’re inside a function, you can use the “:=” short assignment statement to declare a variable implicitly instead of using the “var.” For instance, the code below shows how you can use the “:=” short assignment statement.

func main () {
var i , j int=1, 2
fmt.Println (i, j, k, sam, janet, ismarried)

What about the basic data types of Golang?
Well, Golang has the following basic data types that you should know:

    •The bool data type. It represents a set of Boolean truth values. It can either be true or false.
    •The string data type. A Golang string is implemented as a set of bytes that store a sequence of characters, using a given character encoding scheme.
    •The int data type. A Golang int data type stores whole numbers of varying sizes. They include the int8 for 8 bits, the int16 for 16 bits, the int32 for 32 bit and the int64 for 64 bits.
    •The uint data types. It represents the unsigned integer types of varying lengths. For instance, the uint8 for 8 bit, the uint16 for 16 bits, the uint32 for 32 bits and the uint64 for 64 bits.
    •The byte. It stores integer values for 8 bits. It’s the same as the uint8.
    •The float32 and the float64. They are used for storing floating point numbers for 32 bits and 64 bits respectively.
    •The complex64 and the complex128 data types. It’s used to represent complex numbers for 64 bits and 128 bits respectively.

Well, we’ve come to the end of variables. Keep on practicing with more variables.

Online Technology Courses Stone River eLearning