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

Overview of Go lang

Arrays in C Language

Data Types in Go language

Creating your first html page

Say Hello world in Go lang

Installing go lang

Arrays in Go lang

String in golang

Decision Making in Go lang

Decision Making Statements