Constant in Go lang

Constant in go lang

  1. Constants are basically variable which value can't change once created.
  2. Constants are created in the same way as variables, the only difference is that to create a variable use var keyword and to create constant use const keyword.

Constant Example-1

package main
import "fmt"
func main() {
const playerName string = "Rahul"
fmt.Println(playerName)
//playerName = "Micky" //compile time error, cannot assign to playerName.
}

Constant Example-2

package main
import "fmt"
func main() {
const METER_PER_FEET = 0.3048
var feet,meter float32
fmt.Print("Enter value in feet...")
fmt.Scan(&feet)
meter = feet * METER_PER_FEET
fmt.Printf("Meter %.2f",meter)

Note:

  1. In the above example, METER_PER_FEET is a constant.
  2. It is suggested that constant name should be written in uppercase, each word separated by an underscore so can be easily identified.

Comments

Popular posts from this blog

String in golang

Arrays in C Language

Literals in Java

Pointers in C Language

Inline V/S Block Level Element

Reserved Words in Java

Identifiers

Data Types in Go language

Printing in C programming

Variable Naming & Scope of Variable