Go Programming Interview Questions - Variables and Constants in GoLang

Опубликовано: 19 Март 2026
на канале: Reza Mirjahan
61
3

four ways to declare variables in Go, including var with explicit type, var with initialization, type inference, and the short declaration operator :=. You will understand where short declaration is allowed and how grouped var blocks are used at package level in idiomatic Go.

00:00 – Variable Declaration Methods (var, type inference, short :=)
00:00:54 – Grouped var Block & Package-Level Declarations
00:01:22 – Declaring Multiple Variables in One Line
00:02:07 – Declaring Constants & Grouped const Blocks
00:02:43 – Compile-Time Rule for Constants
00:03:21 – Allowed Constant Types in Go
00:03:54 – Untyped Constants & Arbitrary Precision
00:04:44 – Using iota for Sequential Constants
00:05:22 – Advanced iota Patterns (Bit Flags, Enums)
00:06:16 – Scope Hierarchy (Universe → Package → File → Block)
00:07:04 – Shadowing & Why It Causes Bugs
00:07:50 – Dangerous err Shadowing Bug
00:08:35 – Shadowing Built-in Identifiers
00:09:20 – Short Declaration Re-declaration Rule
00:10:10 – Scope of Variables Inside for Loops
00:10:50 – Go 1.22 Loop Variable Change (Closure Fix)
00:11:56 – Variables in if / switch Initialization
00:12:38 – Exported vs Unexported Identifiers (Capitalization Rule)
00:13:36 – Unused Variables Rule & Blank Identifier _
00:14:34 – Unused Imports & Blank Import
00:15:22 – Type Inference with Short Declaration :=
00:16:15 – Constant Folding (Zero Runtime Cost)
00:17:06 – No Implicit Type Conversion in Go
00:17:54 – Blank Identifier _ Use Cases
00:18:38 – = vs := (Common Mistakes)
00:19:21 – Constant Limitations vs Other Languages
00:20:04 – Mixed New & Existing Variables with :=
00:20:50 – init() Function Execution Order
00:21:37 – Type-Safe Enums with Custom Types
00:22:15 – String Immutability & Byte Slice Conversion
00:23:00 – Linting, Tooling & Go vs Other Languages

We cover declaring multiple variables at once, zero values, and how type inference works at compile time. You will also learn how Go determines types when using := and why there is no automatic type conversion between integers, floats, and other types.

The video explains constants in detail: how to declare them, compile time evaluation rules, and why constants must be known at compile time. You will see why function calls like math.Sqrt or time.Now cannot be used in constants, and which types are allowed for constants: boolean, numeric, and string only.

We explore typed vs untyped constants, arbitrary precision, constant folding, and how untyped constants can adapt to different compatible types. You will also master iota, including basic sequences, bit flags, custom formulas, skipping values, enum like patterns, and type safe enums using custom types.

A large section is dedicated to scope and shadowing. You will understand universe scope, package scope, file scope, and block scope. We explain shadowing bugs, especially the dangerous err shadowing issue with := inside inner blocks, and how to fix it correctly.