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"
//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...")
meter = feet * METER_PER_FEET
fmt.Printf("Meter %.2f",meter)


  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.


Popular posts from this blog

Program to calculate telephone bill

Overview of Go lang

Arrays in C Language

String in golang

Data Types in Go language

Say Hello world in Go lang

Installing go lang

Arrays in Go lang

Decision Making in Go lang

Creating your first html page