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

Overview of Go lang

Arrays in C Language

Program to calculate telephone bill

String in golang

Data Types in Go language

Installing go lang

Say Hello world in Go lang

Creating your first html page

Arrays in Go lang

Decision Making in Go lang