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

Program to calculate telephone bill

Sum of two numbers in go lang

Program to check a year is leap year or not.

String in golang

Identifiers

Arrays in C Language

Reserved Words in Java

Data types in Java

Pointers in C Language

Literals in Java