Golang Tutorial

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

Golang program to demonstrate the 'const' keyword

Here, we are going to demonstrate the 'const' keyword in Golang (Go Language)? By Nidhi Last updated : March 28, 2023

The const keyword in Golang

The const keyword is used to declare a constant in Golang. The constants are declared like variables, but with the const keyword and the constant value. Following types can be declared as constants,

  • Character
  • String
  • Boolean
  • Numeric values

In this program, we will create two constants using the "const" keyword. We cannot modify the value of constants during the program.

Golang code to demonstrate the example of const keyword

The source code to demonstrate the const keyword is given below. The given program is compiled and executed successfully.

// Golang program to demonstrate the "const" keyword

package main

import "fmt"

func main() {
	const str string = "India"
	const intval int = 108
	const c byte = 'x'

	//Constant value cannot modify, will generate error.

	fmt.Printf("String: %s", str)
	fmt.Printf("\nValue: %d", intval)
	fmt.Printf("\ncharacter: %c", c)


String: India
Value: 108
character: x


In the above program, we declare the package main. The main package is used to tell the Go language compiler that the package must be compiled and produced the executable file. Here, we imported the fmt package that includes the files of package fmt then we can use a function related to the fmt package.

In the main() function, we created a string, an integer and a byte constant, which are initialized with "India", 108, 'x'. As we know that, we cannot modify the value of constants during the program. If we assign a new value to the constant then syntax error gets generated.

Golang Basic Programs »

Related Programs

Comments and Discussions!

Load comments ↻

Copyright © 2024 www.includehelp.com. All rights reserved.