123ArticleOnline Logo
Welcome to 123ArticleOnline.com!
ALL >> Computer-Programming >> View Article

Mastering Go: Guide To Type Declarations, Variables, And Constants

Profile Picture
By Author: Sanaya
Total Articles: 48
Comment this article
Facebook ShareTwitter ShareGoogle+ ShareTwitter Share

Constants in Go
In Go, constants are fixed values that are known at compile time and remain unchanged throughout the program’s execution. They are defined using the const keyword and can be of basic types like integers, floats, strings, and booleans. Constants help improve code readability and maintainability by ensuring certain values remain consistent.

Declaring Constants
Constants are declared using the const keyword, followed by the constant's name, type, and value. The type can often be omitted because Go infers it from the assigned value.

const Pi float64 = 3.14159
const Greeting = "Hello, World!"
const DaysInWeek = 7
const IsTrue = true
Grouping Constants
You can group constants using parentheses, which is useful for organizing related constants.

const (
Pi = 3.14159
Greeting = "Hello, World!"
DaysInWeek = 7
)
Untyped Constants
Go allows untyped constants, which do not have a fixed type until they are used in a context that requires a specific type. This provides flexibility.

const Pi = 3.14159 // untyped ...
... constant
var radius float64 = 10
var circumference = 2 * Pi * radius // Pi is used as a float64 here
Enumerated Constants
Enumerated constants are a way to create a sequence of related constants, often using the iota keyword, which simplifies creating incrementing values.


const (
Sunday = iota // 0
Monday // 1
Tuesday // 2
Wednesday // 3
Thursday // 4
Friday // 5
Saturday // 6
)
The iota keyword represents successive integer constants starting from 0 and resets to 0 whenever the const keyword appears.

Constants with Expressions
Constants can be defined using expressions as long as the result is a compile-time constant.

const (
SecondsInMinute = 60
MinutesInHour = 60
HoursInDay = 24
SecondsInDay = SecondsInMinute * MinutesInHour * HoursInDay // 86400
)
Limitations of Constants
Immutable: Constants cannot be changed once defined.

Compile-Time: The value of a constant must be known at compile time.

Basic Types Only: Constants can only be of basic types. More complex types like slices, maps, and structs cannot be constants.

Practical Usage of Constants
Constants are often used to define values that are used multiple times in the code, such as configuration settings, fixed dimensions, or commonly used strings.


package main

import "fmt"

const (
Pi = 3.14159
AppName = "MyApp"
MaxUsers = 100
WelcomeMsg = "Welcome to MyApp!"
)

func main() {
fmt.Println(AppName, "allows up to", MaxUsers, "users.")
fmt.Println(WelcomeMsg)
fmt.Printf("The value of Pi is approximately %.2f\n", Pi)
}

Total Views: 187Word Count: 468See All articles From Author

Add Comment

Computer Programming Articles

1. Best Crm Features Every Small Business Should Use In 2025
Author: Aimbeat Insights

2. Why Learn Data Science For 2026?
Author: TCCI - Tririd Computer Coaching Institute

3. Full Stack Developer Training Iskcon Cross Road: Join Tcci Now
Author: TCCI - Tririd Computer Coaching Institute

4. Top 5 Crm Systems That Integrate With Google Workspace
Author: Orson Amiri

5. It Career Guidance: Free Counseling At Tcci Institute
Author: TCCI - Tririd Computer Coaching Institute

6. Data Structure & Algorithms (dsa): Tcci Coaching
Author: TCCI - Tririd Computer Coaching Institute

7. The Copilot Effect: 7 Real Productivity Wins Businesses Are Seeing Right Now
Author: ECF Data

8. Ultimate Cucumber Testing Tutorial For Testers: Concepts, Workflow & Advantages
Author: Tech Point

9. Postman Tutorial: How To Test Apis Quickly And Efficiently
Author: Tech Point

10. Learn Python Programming South Bopal, Ahmedabad
Author: TCCI - Tririd Computer Coaching Institute

11. Job-oriented It Training In Ahmedabad: Why Tcci?
Author: TCCI - Tririd Computer Coaching Institute

12. Core Java Classes Near Iskcon Circle: Tcci Expert Faculty
Author: TCCI - Tririd Computer Coaching Institute

13. Why Learning Javascript Is Still King In Web Dev
Author: TCCI - Tririd Computer Coaching Institute

14. Data Science Course Near South Bopal: Best Computer Classes
Author: TCCI - Tririd Computer Coaching Institute

15. From Zero To Coder: Tcci's Programming Roadmap
Author: TCCI - Tririd Computer Coaching Institute

Login To Account
Login Email:
Password:
Forgot Password?
New User?
Sign Up Newsletter
Email Address: