Lädt...


🔧 Mastering Go: Guide to Type Declarations, Variables, and Constants


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

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)
}

Conclusion

Constants in Go are a useful feature for defining fixed values, ensuring consistency, and preventing accidental changes. By understanding how to declare and use constants, including untyped constants and iota for enumerations, you can write more maintainable and error-resistant Go programs.

...

🔧 Mastering Go: Guide to Type Declarations, Variables, and Constants


📈 87.29 Punkte
🔧 Programmierung

📰 Python Type Hinting: From Type Aliases To Type Variables and New Types


📈 41.68 Punkte
🔧 AI Nachrichten

🔧 Variables and Constants: Declaration and Usage


📈 39.57 Punkte
🔧 Programmierung

📰 Rust Basics Series #2: Using Variables and Constants in Rust Programs


📈 37.94 Punkte
🐧 Unix Server

🔧 Variables, Shadowing, and Constants in Rust


📈 37.94 Punkte
🔧 Programmierung

🔧 Swift 101: Understanding Types, Variables, and Constants


📈 37.94 Punkte
🔧 Programmierung

🔧 Static Variables, Constants, and Methods


📈 37.94 Punkte
🔧 Programmierung

🔧 Variables, Constants, Data Types, and Namespaces in C++


📈 37.94 Punkte
🔧 Programmierung

🔧 Task 2 - Constants and variables


📈 37.94 Punkte
🔧 Programmierung

🔧 PYTHON-FUNDAMENTALS: CONSTANTS, VARIABLES AND DATA TYPES


📈 37.94 Punkte
🔧 Programmierung

🔧 What are Variables and Constants in Go? Explained With Examples


📈 37.94 Punkte
🔧 Programmierung

🔧 10/7/24 - Day 2 - Data types,variables,constants


📈 36.31 Punkte
🔧 Programmierung

🔧 July 10, 2024 Python | DataTypes, Variables, Constants


📈 36.31 Punkte
🔧 Programmierung

🔧 A Beginner's Guide to Variable Declarations in JavaScript: When to Use const, let, and var


📈 32.96 Punkte
🔧 Programmierung

🔧 Mastering Variables and Scope in JavaScript: A Developer's Guide


📈 30.4 Punkte
🔧 Programmierung

🔧 Mastering Environment Variables in Node.js: A Step-by-Step Guide Using DotEnv


📈 28.77 Punkte
🔧 Programmierung

🔧 Mastering CSS Variables: A Beginner's Guide to Custom Properties


📈 28.77 Punkte
🔧 Programmierung

🔧 Refactoring instance variables to local variables in Rails controllers


📈 27.88 Punkte
🔧 Programmierung

📰 Explainer: National Emergency Declarations and COVID-19


📈 27.45 Punkte
📰 IT Security Nachrichten

🎥 AI Declarations and AGI Timelines – Looking More Optimistic?


📈 27.45 Punkte
🎥 Künstliche Intelligenz Videos

🔧 Comparing Lexical Scope for Function Declarations and Arrow Functions


📈 27.45 Punkte
🔧 Programmierung

🔧 Mastering Type Guards in TypeScript: Ensuring Safe Type Checks


📈 26.73 Punkte
🔧 Programmierung

🕵️ CVE-2022-45122 | Movable Type 7/Type Premium/Type Premium Advanced cross site scripting


📈 26.11 Punkte
🕵️ Sicherheitslücken

🕵️ CVE-2022-45113 | Movable Type 7/Type Premium/Type Premium Advanced URL input validation


📈 26.11 Punkte
🕵️ Sicherheitslücken

🕵️ CVE-2022-43660 | Movable Type 7/Type Premium/Type Premium Advanced os command injection


📈 26.11 Punkte
🕵️ Sicherheitslücken

🎥 Breaking down destructuring declarations - Kotlin Vocabulary


📈 25.82 Punkte
🎥 Video | Youtube

🔧 C# Language Highlights: Using Declarations | On .NET


📈 25.82 Punkte
🔧 Programmierung

🔧 Function declarations & Function expressions


📈 25.82 Punkte
🔧 Programmierung

🔧 Variable Declarations in JavaScript


📈 25.82 Punkte
🔧 Programmierung

🔧 Musings on C & C++ Declarations


📈 25.82 Punkte
🔧 Programmierung

🔧 How I organize my CSS declarations 🗂️


📈 25.82 Punkte
🔧 Programmierung

🔧 Understanding JavaScript Function Declarations: 'function' vs 'const'


📈 25.82 Punkte
🔧 Programmierung

🔧 Simplify Customs Declarations with CDS: 5 Easy Steps


📈 25.82 Punkte
🔧 Programmierung

🔧 function declarations vs function expressions in Javascript 🤔✌️


📈 25.82 Punkte
🔧 Programmierung

🔧 Angular @let declarations: Smart Template Subscriptions


📈 25.82 Punkte
🔧 Programmierung

matomo