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
How to add Watermark or Merge two image?How to check if a string contains a substring in Golang?How to find the type of the variable by different ways in Golang?Example: How to use TeeReader from IO Package in Golang?Get current date and time in various format in golangSplit URL and Get Parameters from URLHow to handle HTTP Get response?Closures Functions in GolangHow can we reverse a simple string in Go?User Defined Function Types 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.