Variables, Data Types And Constants in Go
Table of contents
Variable
A variable is a symbolic name given to the storage location which contains some value that can be changed at any time during the execution of the program. A variable must be defined with the type of data or value it is holding.
Data types
There are several data types in Go.
bool int uint float32 complex64
string int8 uint8 float64 complex128
byte int16 uint16
rune int32 uint32
error int64 uint64
Zero value
In some programming languages variable holds a null
or undefined
value when not initialized, Go gives it a zero value of its data type. A boolean
variable if not initialized, get the false
value and an integer
variable gets 0
value, string
variable will get ""
(empty) value.
Declaring a variable
The var
the statement declares a list of variables. The type is at the last of the statement.
var varName dataType
E.g.:
var i, j, k int
var f float32
var u uint
Variables with initializers
We can initialize variables while declaring.
var varName dataType = value
var i int = 10
var s string = "hello"
var ok bool = true
We can omit the type of variable, it will take the type of initializers
var i = 42 // int
var f = 3.142 // float64
var g = "hello" // string
Short-hand declaration
This is the most commonly used variable declaration inside functions.
varName := value
E.g.:
i := 10
s := "hello"
ok := true
When declaring a variable without specifying it’s type the variable’s type is inferred from the value on the right-hand side.
var i int
j := i // j is an int
i := 42 // int
f := 3.142 // float64
g := 0.867 + 0.5i // complex128
Assign or Re-assign
You can assign or re-assign value to a declared variable as:
varName = value
E.g.:
i = 12
s = "world"
ok = false
Multiple variable declarations
Multiple variables of the same type with the single var statement
var i, j, k int
var x, y, z = 0.867 + 0.5i, 4.65, true
Multiple variables of the different types with the single var statement
var (
i int = 10
s string = "hello"
ok bool = true
)
Constants
Go supports constants of character, string, boolean, and numeric values. Constants are declared using the const
keyword.
E.g:
const str string = "constants"
Untyped and Typed
Constants can be declared with or without a type in Go. If we are declaring a literal constant, then we are declaring constants that are untyped and unnamed.
E.g.:
const str string = "constants" //Typed constant
const i = 10 //Untyped constant, literal declaration
const f = 3.14
The constants on the LHS of the declaration are named constants and the literal values on the RHS are unnamed constants.
Typed constants don’t use the same type system as variables, they've their implementation for representing the values that we associate with them.
In the case of a typed constant, the declared type is used to associate the type’s precision limitations or kind of constant.
In the case of an untyped constant, the literal value will determine what kind/type the constant takes
Every GO compiler has the flexibility to implement constants as they wish, within the mandatory set of requirements.
Thank you for reading this blog, and please give your feedback in the comment section below.
Subscribe to my newsletter
Read articles from Pratik Jagrut directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Pratik Jagrut
Pratik Jagrut
👋 Hey there! I'm a community-driven software engineer, and I absolutely love diving into the world of cloud-native development and exploring the endless possibilities of open-source technologies. 💻 My skill set revolves around Kubernetes, GoLang, Python, Docker, and other fascinating container technologies. 🚀 And hey, just to add to the mix, I'm also CKA certified! 🎓 But you know what? My journey doesn't stop at coding. I have a genuine passion for technical evangelism. 🎤 I've had the privilege to speak at world-renowned events, sharing my knowledge and insights with others. It's such an exhilarating experience! And when I'm not on stage, you can find me pouring my thoughts into engaging technical blogs. 📝 If you've made it this far and you're intrigued by what you've read, let's not leave it at that! Reach out, and let's connect. Who knows what exciting opportunities may be awaiting us? 😊