xxxxxxxxxx
package main
import "fmt"
func variables() {
//constant
const APP_NAME string = "NEW APP"
// first define the name then the type
var age int32 = 10
// group of variables
var (
name string = "Himanshu"
class int32 = 19
)
// over write a variable
name = "Himanshu Jangid"
// declare multiple variables
x, y := 10, 20
println("Multiple Variables", name, class, x)
// pointers
var p *int = &x
println(x, p)
println("Before Swap", x, y)
swap(&x, &y)
println("After Swap", x, y)
// we can also print without fmt
println(APP_NAME, age)
var my_name = "Some Name"
println("We Have This many chars: ", len(my_name))
fmt.Printf("typeof my name is : %T \n", my_name)
}
// swap values via pointers
func swap(x *int, y *int) {
*x, *y = *y, *x
}
xxxxxxxxxx
package main
import "fmt"
var c, python, java bool
func main() {
var i int
fmt.Println(i, c, python, java)
}
xxxxxxxxxx
package main
import "fmt"
var i, j int = 1, 2
func main() {
var c, python, java = true, false, "no!"
fmt.Println(i, j, c, python, java)
}
xxxxxxxxxx
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)
}
xxxxxxxxxx
package main
import "fmt"
func main() {
number := 100
decimal := 98.6
phrase := "Hello World"
result := true
}