Types of Constants in Go

Types of Constants in Go

In this tutorial, we are going to discuss types of constants in Go language. Go language supports mainly 3 types of constants.

  • String
  • Numeric
  • Boolean
Types of Constants in Go
1. String Constant

In go language, string constant is represented in 2 ways.

  • Any value that is enclosed between double quotes
  • Any value enclosed between back quotes

Below program shows a example of a

  • Typed string constant
  • Untyped unnamed string constant
  • Untyped named string constant
package main

import "fmt"

func main() {
	type myString string

	//Typed String constant
	const name string = "Ashok"
	var myName = name
	fmt.Println("Untyped named string constant")
	fmt.Printf("myName: Type: %T Value: %v\n\n", myName, myName)

	//Below line will raise a compilation error
	//var author myString = name

	//Untyped named string constant
	const place = "Hyderabad"
	var Place myString = place
	var myPlace = place
	fmt.Println("Untyped named string constant")
	fmt.Printf("Place: Type: %T Value: %v\n", Place, Place)
	fmt.Printf("myPlace: Type: %T Value: %v\n\n", myPlace, myPlace)

	//Untyped unnamed string constant
	var website myString = "Waytoeasylearn"
	var myWebsite = "Waytoeasylearn"
	fmt.Println("Untyped unnamed string constant")
	fmt.Printf("website: Type: %T Value: %v\n", website, website)
	fmt.Printf("myWebsite: Type: %T Value: %v\n", myWebsite, myWebsite)

}

Output

Untyped named string constant
myName: Type: string Value: Ashok

Untyped named string constant
Place: Type: main.myString Value: Hyderabad
myPlace: Type: string Value: Hyderabad

Untyped unnamed string constant
website: Type: main.myString Value: Waytoeasylearn
myWebsite: Type: string Value: Waytoeasylearn

Run in playground

Numeric Constant

Numeric constant are further divided into three types

  • Integer
  • Floats
  • Complex Numbers

An untyped integer constant (both named and unnamed) can be assigned to int types, float types and complex. This is because an int value can be int or float or complex. 

For example int value 123 can be

  • A int whose value is 123
  • A float whose value is 123.0
  • A complex whose imaginary part is 0

Based on similar logic, an untyped float constant can be assigned to all floats and complex types but not integer type because, for example, a float 5.3 cannot be an integer.

Based on similar logic, an untyped complex constant can be assigned to complex types but not integer and float because, for example, a float 5i+3 cannot be an integer or a float type.

Boolean Constant

There are two untyped boolean constants, true and false. Below is the program illustrating a boolean constant. These are similar to string constants. It applies the same rules as a string constant. The difference is only that it has two untyped constants, true and false.

package main

import "fmt"

func main() {
	const trueConst = true
	type myBool bool
	var defaultBool = trueConst       // allowed
	var customBool myBool = trueConst // allowed
	// defaultBool = customBool       // not allowed
	fmt.Println(defaultBool)
	fmt.Println(customBool)
}

Output

true
true

Run in playground

That’s all about the Types of Constants in Go language. If you have any queries or feedback, please write us email at contact@waytoeasylearn.com. Enjoy learning, Enjoy Go language.!!

Types of Constants in Go
Scroll to top