Golang Tutorial
Introduction Variables Constants Data Type Convert Types Operators If..Else Switch..Case For Loops Functions Variadic Functions Deferred Functions Calls Panic and Recover Arrays Slices Maps Struct Interface Goroutines Channels Concurrency Problems Logs Files and Directories Reading and Writing Files Regular Expression Find DNS records Cryptography Gotchas in Golang Import and Export Best Golang Packages Web Application Goroutines and Channels Exercises Reflection in Golang Golang for beginners Strings in Golang HTTP Client Server Examples Context PackageGolang Reference
Basic Programs Advance Programs Data Structure and Algorithms Date and Time Slice Sort, Reverse, Search Functions String Functions Methods and Objects Interface TypeBeego Framework
Beego Setup Beego Database Migration Beego GET POST Beego RoutingConstants in Go
Constants
A constant is a name or an identifier for a fixed value. The value of a variable can vary, but the value of a constant must remain constant.
Declaring (Creating) Constants
The keyword const is used for declaring constants followed by the desired name and the type of value the constant will hold. You must assign a value at the time of the constant declaration, you can't assign a value later as with variables.
Example
package main
import "fmt"
const PRODUCT string = "Canada"
const PRICE = 500
func main() {
fmt.Println(PRODUCT)
fmt.Println(PRICE)
}
You can also omit the type at the time the constant is declared. The type of the value assigned to the constant will be used as the type of that variable.
Multilple Constants Declaration Block
Constants declaration can to be grouped together into blocks for greater readability and code quality.
Example
package main
import "fmt"
const (
PRODUCT = "Mobile"
QUANTITY = 50
PRICE = 50.50
STOCK = true
)
func main() {
fmt.Println(QUANTITY)
fmt.Println(PRICE)
fmt.Println(PRODUCT)
fmt.Println(STOCK)
}
Naming Conventions for Golang Constants
- Name of constants must follow the same rules as variable names, which means a valid constant name must starts with a letter or underscore, followed by any number of letters, numbers or underscores.
- By convention, constant names are usually written in uppercase letters. This is for their easy identification and differentiation from variables in the source code.
Most Helpful This Week
Normal function parameter with variadic function parameterHow to Draw a rectangle in Golang?How to copy a map to another map?How to import structs from another package in Go?Example: Split, Join, and Equal from BYTES PackageRuntime package variablesHow to find the type of the variable by different ways in Golang?Golang String ConcatenationHow to convert Colorful PNG image to Gray-scale?How to extract text from between html tag using Regular Expressions in Golang?
Golang Programs is designed to help beginner programmers who want to learn web development technologies, or start a career in website development. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content.