#03 Variables & Constants

Search for a command to run...

No comments yet. Be the first to comment.
I am learning the Go language and taking notes while learning in the easiest way from my side and in detail, I will just post here by Series.
Go language statically typed language, compiled programming language designed at Google. Infrastructure has changed a lot in years with cloud and multi-core processors. As it said the infrastructure is scalable & distributed, and dynamic has more cap...
A go file has the following parts: Package declaration Import packages main() function Example: package main import "fmt" func main() { fmt.Println("Hello World!") } package keyword defines the package name because every program is a part of t...

Content Security Policy ( CSP ) CSP is a layer that helps to prevent certain types of attacks including Cross-Site Scripting ( XSS ) and data injection attacks. These types of attacks are used to deface websites and steal data. CSP is designed to be ...

Go language statically typed language, compiled programming language designed at Google. Infrastructure has changed a lot in years with cloud and multi-core processors. As it said the infrastructure is scalable & distributed, and dynamic has more cap...

Statically Typed Languages Statically typed language needs to define variable data type before compiling the program Because the compiler doesn’t understand the type of variable value whether it’s string or integer. Example: public class Main { p...

Variables are for storing data values. In Golang there are available data types for variables are string, int, boolean, and float32.
There are 2 ways to declare the variable
First way is to declare a variable using the var keyword
:= signSyntax
var variablename type = value
Declaring variable needs to specify with type or value.
Example:
package main
import ("fmt")
func main() {
var a
fmt.Println(a)
// output: syntax error: unexpected newline, expecting type
}
If we define the variable without type it will return an error, which means when we don’t want to provide value it requires defining the type of the variable. The correct way to write variable without value is
package main
import ("fmt")
func main() {
var a int
fmt.Println(a)
// output: 0
}
Now coming back to the var keyword to how to declare a variable with the syntax:
package main
import ("fmt")
func main() {
var variable_1 string = "Hello world!"
fmt.Println(variable_1)
// output: Hello world!
}
Another way is to declare a variable with the := sign, It’s syntax is
variablename := value
When a programmer will use the := sign they will need to assign a value of the variable or not it will return a program error.
package main
import ("fmt")
func main() {
a :=
fmt.Println(a)
// output: assignment mismatch: 1 variable but fmt.Println returns 2 values
// output: undefined: a
}
Writing an example program to work with := sign
package main
import ("fmt")
func main() {
a := true
fmt.Printf("Value: %t, Type: %T", a, a)
// output: Value: true, Type: bool
}
You can check the format type from fmt formatting. There is another way to declare variables with the below syntax
var variablename = value
Above syntax and := sign type variable is inferred meaning that the compiler decides the type of the variable, based on the value of the variable.
Example:
package main
import ("fmt")
func main() {
var var_1 string = "String 1"
var var_2 = "String 2"
var_3 := "String 3"
fmt.Printf("%s, %s, %s", var_1, var_2, var_3)
// Output: String 1, String 2, String 3
}
There are differences between var and :=
| var | := |
| Can be used inside and outside functions | Can only be used inside functions |
| Variable declaration and value assignment can be done separately | Variable declaration and value assignment can not be done separately |
The const keyword declares the variable as "constant” that’s means the constant variable is only read-only and unchangeable.
Example Syntax:
const CONSTNAME type = value
Example:
package main
import ("fmt")
const PI = 3.14
func main() {
fmt.Println(PI)
}
Two types of constant
The typed constant is basically declaring the data type for the constant.
Example:
package main
import ("fmt")
const a_var int = 10
func main() {
fmt.Println(a_var)
}
The Untyped constant is not declaring the data type for the constant.
Example:
package main
import ("fmt")
const b_var = 20
func main() {
fmt.Println(b_var)
}