Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

Go Constants


Go Constants

If a variable should have a fixed value that cannot be changed, you can use the const keyword.

The const keyword declares the variable as "constant", which means that it is unchangeable and read-only.

Syntax

const CONSTNAME type = value

Note: The value of a constant must be assigned when you declare it.


Declaring a Constant

Here is an example of declaring a constant in Go:

Example

package main
import ("fmt")

const PI = 3.14

func main() {
  fmt.Println(PI)
}
Try it Yourself »

Constant Rules

  • Constant names follow the same naming rules as variables
  • Constant names are usually written in uppercase letters (for easy identification and differentiation from variables)
  • Constants can be declared both inside and outside of a function

Constant Types

There are two types of constants:

  • Typed constants
  • Untyped constants

Typed Constants

Typed constants are declared with a defined type:

Example

package main
import ("fmt")

const A int = 1

func main() {
  fmt.Println(A)
}
Try it Yourself »


Untyped Constants

Untyped constants are declared without a type:

Example

package main
import ("fmt")

const A = 1

func main() {
  fmt.Println(A)
}
Try it Yourself »

Note: In this case, the type of the constant is inferred from the value (means the compiler decides the type of the constant, based on the value).


Constants: Unchangeable and Read-only

When a constant is declared, it is not possible to change the value later:

Example

package main
import ("fmt")

func main() {
  const A = 1
  A = 2
  fmt.Println(A)
}

Result:

./prog.go:8:7: cannot assign to A
Try it Yourself »

Multiple Constants Declaration

Multiple constants can be grouped together into a block for readability:

Example

package main
import ("fmt")

const (
  A int = 1
  B = 3.14
  C = "Hi!"
)

func main() {
  fmt.Println(A)
  fmt.Println(B)
  fmt.Println(C)
}
Try it Yourself »

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.