Data Type

  1. Difference Between := and =

:= used for variable declaration and inisialisation, while "=" used only for variable declaration.

package main

import "fmt"

func main() {
    a := 5
    fmt.Println(a)

    var b int
    b = 3
    fmt.Println(b)
}
  1. Convert Float to Integer

package main

import "fmt"

func main() {
    a := 5.5
    fmt.Println(int(a))
}
  1. Check Variable Type

package main

import "fmt"

func do(i interface{}) {
	switch v := i.(type) {
	case int:
		fmt.Printf("%v is an integer: \n", v)
	case string:
		fmt.Printf("%v is a string : \n", v)
	case float32:
		fmt.Printf("%v is a float32!\n", v)
	case float64:
		fmt.Printf("%v is a float64!\n", v)
	case bool:
		fmt.Printf("%v is a boolean!\n", v)
	default :
	    fmt.Printf("don't know\n")
	}
}

func main() {
	do(3)
	do(5.5)
	do("hi")
	do(true)
}
  1. Concatenate String

package main
import "fmt"
  
func main() { 
    var str1 string 
    str1 = "Hello "
    var str2 string 
    str2 = "Reader!"
    fmt.Println("String 1: ", str1+str2) 
  
    str3 := "Hai,"
    str4 := "to meet you"
    result := str3 + " nice " + str4 
    fmt.Println("String 2: ", result) 
} 

Last updated