C#
GO

C# to Go

10 lessons

Progress0%
1Introduction2Type System3Classes to Structs4Interfaces5Error Handling6Async to Goroutines7Generics8LINQ to Slices9Testing10Packages and Modules
All Mirror Courses
C#
GO
Type System
MirrorLesson 2 of 10
Lesson 2

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.

Mirror Card
C#
From C#:

In C#, you're familiar with type system.

GO
In Go:

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:

GO
Go 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        // nil

Comparing to C#

Here's how you might have written similar code in C#:

C#
C# (What you know)
int x = 10;
var y = 20;         // inferred
string name = "Go";
bool flag = true;

// Zero / default values
int zero;           // default 0
string empty;       // default null
Mirror Card
C#
From C#:

You may be used to different syntax or behavior.

GO
In Go:

Go short declaration := infers type — similar to C# var but only inside functions

Mirror Card
C#
From C#:

You may be used to different syntax or behavior.

GO
In Go:

Go zero values: int=0, string="", bool=false, pointer=nil — never uninitialized

Mirror Card
C#
From C#:

You may be used to different syntax or behavior.

GO
In Go:

string zero value is "" (empty string), not null — no null reference exceptions for strings

Mirror Card
C#
From C#:

You may be used to different syntax or behavior.

GO
In Go:

Go type comes after variable name: var x int vs C# int x

Mirror Card
C#
From C#:

You may be used to different syntax or behavior.

GO
In Go:

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.

C#
C#
int x = 5;
var y = 10;       // inferred
string s = "hi";
GO
Go
var x int = 5
var y = 10        // inferred as int
x := 5            // short form — same result

var s string = "hi"
s := "hi"         // equivalent
Common Pitfall
:= cannot be used at the package level — only inside function bodies. Use var for package-level declarations.

2. 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.

C#
C#
int x;      // C#: 0 (struct) or must assign
string s;   // C#: null — NullReferenceException risk
GO
Go
var 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}
Rule of Thumb
Go's zero values make default initialization safe. Design your types so the zero value is meaningful and usable.

3. No Implicit Conversions

Go requires explicit type conversions everywhere — even between int32 and int64. This is stricter than C#.

C#
C#
int x = 5;
long y = x;          // implicit widening — OK in C#
double d = x;        // implicit — OK in C#
GO
Go
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)!
Common Pitfall
string(65) in Go does NOT produce "65". It produces the Unicode character at code point 65 ("A"). Use fmt.Sprintf("%d", n) or strconv.Itoa(n) to convert numbers to strings.

4. Constants and iota

Go constants use const. iota is a special incrementing counter for enums — it replaces C# enum declarations.

C#
C#
const int MaxRetries = 3;
enum Direction { North, South, East, West }
GO
Go
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
Common Pitfall
Don't assume Go works exactly like C#. While the concepts may be similar, the syntax and behavior can differ significantly.

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
Rule of Thumb
The best way to learn is by doing. Try rewriting some of your C# code in Go to practice these concepts.
PreviousNext