Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package main
import (
"fmt"
"strconv"
)
// k:=6 not possible in global scope
var (
// lowercase 1st letter var are scoped to the package
k int8 = 6
fname string = "Simrajneet"
lname string = "Singh"
// uppercase 1st letter expose to outer levels
Number int = 99
)
func swap(x, y string) (string, string) {
return y, x
}
func main() {
// variables --------------------
// block scope only
var i float64 = 65.6
fmt.Println("Hello, ॐ ,", i)
j := 84
fmt.Printf("\nj = %v -> %T", j, j)
j = int(i)
fmt.Printf("\nj = %v -> %T", j, j)
fmt.Printf("\n\nk = %v -> %T", k, k)
k := 6.4 // can redeclare for type convertion in diff scope i.e shadow them
fmt.Printf("\nk = %v -> %T", k, k)
var str string
// str = string(j)
// fmt.Printf("\n\nstr=%v -> %T (to ASCII)", str, str)
str = strconv.Itoa(j)
fmt.Printf("\nstr = %v -> %T (not ASCII)", str, str)
fmt.Printf("\n\n%v -> %T", fname, fname)
// booleans ----------------
var n bool // all var by default = 0
fmt.Printf("\nn = %v -> %T", n, n)
m := 1 == 1
fmt.Printf("\nm = %v -> %T\n", m, m)
var a, b string = swap("hello", "world")
fmt.Println(a, b)
// Constants cannot be declared using the := syntax.
const w, q int = 1, 2
const c, python, java = true, false, "no!"
fmt.Println(w, q, c, python, java)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment