Golang Variables, Data Types and Const Variable

Golang Variables, Data Types and Const Variable

#GO-getter series

Variable Declaration

Variables, as available in other languages, are the containers used to store the values of different types which will be manipulated throughout use before they finally persist. If you are coming from different languages such as Python, Java or C++, you must be aware of the ways to declare a variable associated with the prefix or suffix that defines data types which decides what type of values (a number, string, boolean etc.) a variable holds.

For instance in Java, variables are declared like type variableName = value that is in a more meaningful way is int a=10. You may or may not want to initialize it. But in Go, the way to declare a variable is like:

The var keyword lists a variable or list of variables that precede variable name x and the type, in our case int is last. There is a red line below variable name, depicting an error that is Go specific error telling us that variable x is declared but not used. To get rid of it, we will just print it using Println function available in fmt package as discussed in https://hashnode.com/post/clnxkaiay000009jm9e0h7iu4.

Unlike Java, uninitialised variables in Go won't throw compilation error, instead it will assign the default value based on the data type of the variable.

Data types

Basic data types in Go as per the documentation are:

Below code snippet shows the variable declaration of different data types with the default value assigned by Go compiler if uninitilized.

Data types that are of numeric type in Go has collection depending on the range of the values we want to store. For example, below code shows the variable of int8 type tries to save a constant value but throws an error untyped int constant.

go variables

As per the Go Language Specification, Go has integer, floating-point, or complex type represents the set of integer, floating-point, or complex values, respectively. They are collectively called numeric types. The predeclared architecture-independent numeric types are:

The range of bit that can be saved in a particular numeric type is shown above. So, in above code int8 type can store the integer value from -128 to 128. To get away with the error, either data type should be large or the number should be in the range of the data type.

There is also a set of predeclared integer types with implementation-specific sizes:

To avoid portability issues all numeric types are defined types and thus distinct except byte, which is an alias for uint8, and rune, which is an alias for int32. Explicit conversions are required when different numeric types are mixed in an expression or assignment. For instance, int32 and int are not the same type even though they may have the same size on a particular architecture.

String data type in Go is very similar to the string type in other languages. A string is a sequence of bytes in which number of bytes is called the length of the string that can never be negative.

Here, we are using Printf function from one of the most used packages in Go that is fmt. Printf uses format verbs that starts with % symbol and replace it with the value of the variable we are providing. In this case %T format verb is used that prints the type of the value we are dealing with. \n at the end denotes the line break as we cannot use Println in combination with printf .

For further information, one can refer to the Go language specification at https://go.dev/ref/spec#Types

Alternative ways to declare a variable

Go facilitates variable declaration with the provision of short hand declaration and it uses := operator. variable_name:=initial_value is the short hand syntax to declare a variable. Variables of different types can be declared as described below.

Here, Go will automatically infer the type of the variable since it has been initialized with some value. While using short hand declaration method variables must be initialized and this syntax is only available inside functions. %v is the special format verb that formats arguments in its default form.

Constants in Go

Constants are the variables but are declared with the const keyword. Constants could be anything like a string, any numeric type or a boolean. Short hand declaration method discussed above can't be used to declare constants. Constant declaration syntax is shown below:

Constant variable isLoggedIn is a package-level variable because it is declared outside of the function and can be accessed through any files in the package it is declared. Constants cannot be changed or reassigned during the execution of a program. Once a constant is declared and assigned a value, it cannot be modified.

There are surely many other ways of variable declaration which we have not discussed here. You can find it while learning by yourself, browsing through Google or by joining the Golang community, forum etc. Happy Learning.