Table of contents
Yesterday we learned about packages and compilation of Go code in depth. Today we are going to learn about variables and constants in Go.
Variables & Constants π₯
Variables are used to store various types of values. Assume it is like a box containing something that is only opened when something is needed from that box. Once the variable is created, you can reference that variable name everywhere in the app.
But you can't easily create variables like x = 29
like you do in Python. There is slight change in Golang. In Go, there are keywords defined to do specific jobs, like if you have noticed in the Hello World program, we used func
before to create a function. func
is a keyword defined to create a function in Go.
So, keywords are specially defined words (or you can say reserved words) to do certain things. You can't use those keywords as your function name or variable name.
For creating a variable, we have a keyword var
here. Let's create a variable:
package main
import "fmt"
func main() {
var challenge = "#90DaysOfDevOps"
var currentDay = "9th day"
fmt.Println("Welcome to", challenge, "")
fmt.Println("We are at", currentDay, "in", challenge)
}
As I said in the starting variables contains various types of values and those types are known as data types.
Go mainly has three basic data-types:
- bool: for boolean values: true or false
- Numeric: represents integers, floats, and other complex values
- String: for string value: character, sentence.
But in the above programme we haven't mentioned any of them. Why? Because Go is smart enough to get the data type passed in the variables by its own. Same like Python.
Constants β
In variables, we can change the value of that variable after it is declared and defined. If you need a variable whose value should be fixed and can't change, then you can use constants.
Like variables, you can define constants using const
keyword.
package main
import "fmt"
func main() {
var challenge = "#90DaysOfDevOps"
var currentDay = "9th day"
const totalDays = 90
fmt.Println("Welcome to", challenge, "")
fmt.Println("We are at", currentDay, "in", challenge)
fmt.Println("Total days are", totalDays)
}
If you try to change the value of a constant, then it will show you this kind of error:
Ending Note π
So that was all for today, see you on next Day.