Back to Gotraining

README

topics/go/language/constants/README.md

latest1.8 KB
Original Source

Constants

Constants are a way to create a named identifier whose value can never change. They also provide an incredible amount of flexibility to the language. The way constants are implemented in Go is very unique.

Notes

  • Constants are not variables.
  • They exist only at compilation.
  • Untyped constants can be implicitly converted where typed constants and variables can't.
  • Think of untyped constants as having a Kind, not a Type.
  • Learn about explicit and implicit conversions.
  • See the power of constants and their use in the standard library.

Constants specification
Constants - Rob Pike
Introduction To Numeric Constants In Go - William Kennedy

Code Review

Declare and initialize constants (Go Playground)
Parallel type system (Kind) (Go Playground)
iota (Go Playground)
Implicit conversion (Go Playground)

Exercises

Exercise 1

Part A: Declare an untyped and typed constant and display their values.

Part B: Divide two literal constants into a typed variable and display the value.

Template (Go Playground) | Answer (Go Playground)


All material is licensed under the Apache License Version 2.0, January 2004.