Skip to content

Instantly share code, notes, and snippets.

@smrnjeet222
Created August 28, 2020 17:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smrnjeet222/f162df6f0aaa3df3b9b8b41410b83090 to your computer and use it in GitHub Desktop.
Save smrnjeet222/f162df6f0aaa3df3b9b8b41410b83090 to your computer and use it in GitHub Desktop.
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