More Go (Go lang learning series blog - 2)

Learn more about Go!
If you've followed and read my first blog of this Learning Go lang series, you can now print any text on your screen using Go lang. Again Congratulations!
Today we'll learn more about Go lang and do a lot of cool stuffs!
Go Variables :
In programming, a variable means a storage of some kind which can store different types of data.
var number intThe above line declares a variable of type int (Integer) which is named number.
Variables can have any names. But there are some rules :
Rules for naming a variable
- A variable name can only have letters (both uppercase and lowercase letters), digits and underscore.
- The first letter of a variable should be either a letter or an underscore.
- There is no rule on how long a variable name (identifier) can be. However, you may run into problems in some compilers if the variable name is longer than 31 characters.
The "var" keyword confirms that number is the name of that particular variable. The int keyword means that the "number" variable can only hold a simple Integer value (whole number, not a fraction).
Let's declare a variable and then assign a value to that.
package main
import "fmt"
func main() {
var number int
number = 12
fmt.Println("The number is : ", number)
}
In the above code we can see that inside the main function we are declaring a variable called number by saying,
var number intIn the next line we are assigning 12 to that variable, now the variable holds 12. So "number" is having the value 12 in it.
In the next line we are printing the number. We learnt that inside fmt.Println(""), whatever we write gets printed. But what about the comma and number after that?
When we want to print a value of a variable, we have write it outside the "" and write a comma after that.
Try running the code above.
The same printing function can be achieved by the following code :
fmt.Printf("The number is : %d\n", number)
Here, Printf does the same thing as Println but is a little bit different.
The %d is a placeholder here which doesn't print %d on the screen but it will be waiting for a variables value which we can write outside the "". The Println function automatically puts an endline or newline after the function so we don't have to type it but in Printf we have to write it manually, which is the \n at the end.
Can the variable be declared in other ways?
Yes! Look at the following code.
package main
import "fmt"
func main() {
number := 12
var name string = "Minhaz"
fmt.Printf("The number is : %d\nThe name is %s\n", number, name)
}
Notice that we're using a new syntax.Which is ,
number := 12
It's called the comma ok syntax. It declares a variable, assign a value to it and automatically sets the data type of the variable.
Also we printed a string type variable. Notice that we are using %s, which is the placeholder for string types in fmt.Printf() function.
Look at the ways we can declare and initialize a variable in Go :
package main
import "fmt"
func main() {
// explicitly declare the data type
var number1 int = 10
fmt.Println(number1)
// assign a value without declaring the data type
var number2 = 20
fmt.Println(number2)
// shorthand notation to define variable
number3 := 30
fmt.Println(number3)
}
Changing Value of a Variable :
We can tell by the name that a variable varies, that means its properties can change. Look at the code below :
package main
import "fmt"
func main() {
// initial value
number := 10
fmt.Println("Initial number value", number) // prints 10
// change variable value
number = 100
fmt.Println("The changed value", number) // prints 100
}
Note that, In Go, we cannot change the type of variables after it is declared.
In the above example, the number variable can only store integer values. It cannot be used to store other types of data. For example,
number := 10
// Error code
// assign string data
number = "Hello"
Creating multiple variables at once :
We can declare as many variables as we like, separating them by commas.
var name, age = "Minhaz", 22
name2, age2 := "minhaz", 22
Both the lines above are valid.
Constants in Go :
Constants are values that cannot be changed after declaration. We define constants as shown below,
const a = 2
// Error! Constants cannot be changed
a = 3 // this line is an error
Data types in Go :
1. Integer Data Type
Integers are whole numbers which can have negative and positive values, but no floating point values or fractional values are alowed in this data type.
There are different types of int in Go :
Data type Size
| int/uint | either 32 bits (4 bytes) or 64 bits (8 bytes) |
| int8/uint8 | 8 bits (1 byte) |
| int16/uint16 | 16 bits (2 bytes) |
| int32/uint32 | 32 bits (4 bytes) |
| int64/uint64 | 64 bits ( 8 bytes) |
Let's code , run and observe following example :
package main
import "fmt"
func main() {
var a, b int
a = 5
b = 10
fmt.Println(a)
fmt.Println(b)
}
2. Float Data Type
Float types are for values with decimal points.
Data type Size
| float32 | 32 bits (4 bytes) | ||
| float64 | 64 bits (8 bytes) |
Let's code , run and observe following example :
package main
import "fmt"
func main() {
var a, b float64
a = 5.456643
b = 10.345
fmt.Println(a)
fmt.Println(b)
}
3. String Data Type
String is a collection of characters, example : hello, my name is minhaz
package main
import "fmt"
func main() {
var a string
a = "Welcome to Minhaz's blog!"
fmt.Println(a)
}
4. Boolean Data Type
Booleans only hold true or false as values. It is declared by bool keyword.
Let's code , run and observe following example :
package main
import "fmt"
func main() {
var boolValue bool
boolValue = false
fmt.Println(boolValue)
}
Ok, let's stop here for today! If you've followed and coded all the examples till this point then kudos to you!
We will meet soon in another blog. Till then Allah hafez!
Comments
Post a Comment