📦 Go Variables, Constants & Data Types – A Complete Beginner’s Guide
🧲 Introduction – Why Variables & Types Matter in Go
In Go (Golang), variables, constants, and data types are foundational. Understanding how to declare variables, use typed constants, and perform type conversions is key to writing reliable, efficient Go code. Go enforces strict typing, which boosts performance and catches bugs at compile-time.
🎯 In this guide, you’ll learn:
- How to declare and initialize variables in Go
- The difference between variables and constants
- Go’s built-in data types and default (zero) values
- How type casting works in Go
📘 Topics Covered
🧱 Topic | 📖 Description |
---|---|
🧮 Go – Variables | Declare, initialize, and update values using var and := syntax |
📌 Go – Constants | Define unchangeable values with the const keyword |
🏷️ Go – Identifiers & Keywords | Understand naming rules and reserved words in Go |
🧊 Go – Data Types | Explore primitive and composite data types in Go |
🔄 Go – Type Casting | Convert between compatible types (e.g., int , float64 ) |
🧮 Go – Variables
🔹 Variable Declaration
var name string
name = "GoLang"
OR shorthand:
age := 30
🧠 Notes:
:=
infers the type automatically (only inside functions).- Variables must be used, or Go will throw a compile-time error.
✨ Multiple Declarations
var x, y int = 10, 20
Shorthand:
a, b := "foo", "bar"
🧪 Default Values (Zero Values)
var count int // 0
var price float64 // 0.0
var status bool // false
var note string // ""
📌 Go – Constants
Use const
for immutable values:
const Pi = 3.14
const Greeting string = "Hello"
✅ Constants must be assigned a value at compile time (no functions or runtime logic).
🏷️ Go – Identifiers & Keywords
✅ Valid Identifiers
- Must start with a letter or
_
- Can contain letters, digits, underscores
var user_name = "Bob"
var UserID123 = 42
🚫 Reserved Keywords
break, const, func, import, package, return, var, type, if, for, switch, etc.
💡 Avoid using keywords as variable names.
🧊 Go – Data Types
🔹 Numeric Types
Type | Size | Example |
---|---|---|
int | Platform-specific | var x int = 10 |
int8 – int64 | Signed integers | var a int32 = 100 |
uint8 – uint64 | Unsigned integers | var b uint = 25 |
float32 , float64 | Floating points | var f float64 = 3.14 |
🔹 Boolean
var active bool = true
🔹 String
var name string = "Gopher"
Strings are UTF-8 and immutable.
🔹 Composite Types (Advanced)
- Arrays:
var arr [3]int = [3]int{1, 2, 3}
- Slices:
nums := []int{4, 5, 6}
- Maps:
m := map[string]int{"a": 1}
- Structs:
type User struct { name string }
🔄 Go – Type Casting
Go does not support implicit conversion. Use explicit casts:
var a int = 10
var b float64 = float64(a)
Converting string to int:
import "strconv"
num, err := strconv.Atoi("42")
✅ Use strconv
for string ↔ number conversion.
📌 Summary – Recap & Next Steps
Go’s variable and type system is minimal but powerful, giving you control over memory, performance, and safety. Mastering these basics sets you up to build robust Go applications.
🔍 Key Takeaways:
- Use
var
or:=
to declare variables - Constants are immutable and typed at compile time
- Go has strict static typing with zero defaults
- Use
strconv
and explicit casts for type conversion - Follow identifier naming rules to avoid errors
⚙️ Real-World Use Cases:
- Initializing request parameters in REST APIs
- Using constants for HTTP codes or config keys
- Performing math, validations, and data transformations
❓ Frequently Asked Questions
❓ Can I declare variables without initializing them?
✅ Yes. They will take a zero value based on type.
❓ What happens if I declare a variable and never use it?
✅ Go will not compile the program. All variables must be used.
❓ Can I change the type of a variable in Go?
✅ ❌ No. Once declared, its type is fixed. You must cast explicitly.
❓ Is :=
allowed outside of functions?
✅ ❌ No. :=
is valid only inside functions. Use var
at the package level.
❓ What’s the default value of a bool
in Go?
✅ false
Share Now :