Type System
Type System
Introduction
In this lesson, you'll learn about type system in Go. Coming from C#, you already have a foundation for understanding this concept. We'll build on that knowledge while highlighting the key differences.
In C#, you're familiar with type system.
Go has its own approach to type system, which we'll explore step by step.
The Go Way
Let's see how Go handles this concept. Here's a typical example:
var x int = 10
y := 20 // short variable declaration
var name string = "Go"
var flag bool = true
// Zero values (Go never has uninitialized variables)
var zero int // 0
var empty string // "" (NOT nil!)
var ptr *int // nilComparing to C#
Here's how you might have written similar code in C#:
int x = 10;
var y = 20; // inferred
string name = "Go";
bool flag = true;
// Zero / default values
int zero; // default 0
string empty; // default nullYou may be used to different syntax or behavior.
Go short declaration := infers type — similar to C# var but only inside functions
You may be used to different syntax or behavior.
Go zero values: int=0, string="", bool=false, pointer=nil — never uninitialized
You may be used to different syntax or behavior.
string zero value is "" (empty string), not null — no null reference exceptions for strings
You may be used to different syntax or behavior.
Go type comes after variable name: var x int vs C# int x
You may be used to different syntax or behavior.
No implicit type conversions in Go — explicit cast required even int32 → int64
Step-by-Step Breakdown
1. Variable Declaration Styles
Go has two declaration forms: var (verbose, works anywhere) and := (short, functions only). Both infer types when an initializer is present.
int x = 5;
var y = 10; // inferred
string s = "hi";var x int = 5
var y = 10 // inferred as int
x := 5 // short form — same result
var s string = "hi"
s := "hi" // equivalent2. Zero Values
In Go every variable is initialized to its zero value. There is no concept of an uninitialized variable. This eliminates an entire class of bugs.
int x; // C#: 0 (struct) or must assign
string s; // C#: null — NullReferenceException riskvar x int // 0 — safe to use immediately
var s string // "" — empty string, never nil
var b bool // false
var p *int // nil — pointer zero value
// Struct zero value: all fields zeroed
var person Person // Person{Name: "", Age: 0}3. No Implicit Conversions
Go requires explicit type conversions everywhere — even between int32 and int64. This is stricter than C#.
int x = 5;
long y = x; // implicit widening — OK in C#
double d = x; // implicit — OK in C#var x int32 = 5
var y int64 = int64(x) // must be explicit
var d float64 = float64(x) // must be explicit
// String conversions
n := 65
ch := string(rune(n)) // "A" — NOT string(65)!4. Constants and iota
Go constants use const. iota is a special incrementing counter for enums — it replaces C# enum declarations.
const int MaxRetries = 3;
enum Direction { North, South, East, West }const MaxRetries = 3
type Direction int
const (
North Direction = iota // 0
South // 1
East // 2
West // 3
)Common Mistakes
When coming from C#, developers often make these mistakes:
- Go short declaration := infers type — similar to C# var but only inside functions
- Go zero values: int=0, string="", bool=false, pointer=nil — never uninitialized
- string zero value is "" (empty string), not null — no null reference exceptions for strings
Key Takeaways
- Use := for short declarations inside functions; var elsewhere
- Every variable has a zero value — no uninitialized state
- No implicit conversions — explicit casts required
- Use iota in const blocks for enum-like sequences