Last active
September 21, 2021 10:39
-
-
Save sarthakpranesh/4b8078c50b034364318cbfdcbe50467d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
) | |
func main() { | |
// Variable declaration with "var" keyword | |
var name string | |
var age int | |
var height float64 | |
var name2 string = "Person" | |
fmt.Printf("Varibles: name=%v, age=%v, height=%v, name2=%v \n", name, age, height, name2) // don't worry we'll cover use of fmt later in the series | |
// reassigning values to variables declared with "var" keyword | |
name2 = "Hugo" | |
name = "Sarthak" | |
age = 22 | |
height = 5.8 | |
fmt.Printf("Reassigned Varibles: name=%v, age=%v, height=%v, name2=%v \n", name, age, height, name2) | |
// below redeclaration of "name" variable will cause error | |
// var name string = "Human"; | |
// Variable declaration with "const" keyword | |
const year int = 2021 | |
// below line will cause an error - reassigning of variable declaraed with const not allowed | |
// year = 2022; | |
// Variable declaration using the Short hand | |
war := true | |
country := "India" | |
i := 3 | |
fmt.Printf("Short variable declaration: war=%v, country=%v, i=%v \n", war, country, i) | |
// Types | |
// Boolean | |
var isGo bool = true | |
fmt.Printf("Boolean: isGo={val:%v, type:%T}\n", isGo, isGo) | |
// Numeric | |
var j int8 = 32 | |
var k uint = 12 // unsigned int, means only positive values allowed, range 0 to 255 | |
var temperature float32 = 43.8 | |
fmt.Printf("Numeric: j={val:%v, type:%T}\nk={val:%v, type:%T}\ntemperature={val:%v, type:%T}\n", j, j, k, k, temperature, temperature) | |
// Strings | |
var text string = "Hello gophers" // strings are immutable, once created cannot be changed | |
fmt.Printf("String: text={val:%v, type:%T}\n", text, text) | |
// custom type | |
type msg string | |
var m msg = "Yo Yo Golang" | |
fmt.Printf("String: msg={val:%v, type:%T}\n", m, m) | |
// Type convertion | |
var t int64 = 12345 | |
fmt.Printf("int: t={val:%v, type:%T}\n", t, t) | |
var tf float64 = float64(t) | |
fmt.Printf("Converted float: msg={val:%v, type:%T}\n", tf, tf) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment