Go Type Parameters 🎯

beginner
25 min

Go Type Parameters 🎯

Welcome to the exciting world of Go Type Parameters! In this lesson, we'll dive deep into understanding how Go handles type parameters and their usage in real-world scenarios. By the end of this tutorial, you'll be able to create flexible functions and methods that can work with various data types.

Table of Contents

  1. Introduction
  2. Understanding Type Parameters
  3. Creating Generic Functions
  4. Using Type Parameters in Methods
  5. Type Parameters and Interfaces
  6. Quiz

<a name="introduction"></a>

1. Introduction 📝

Type parameters, also known as generic types, allow us to create functions or methods that can work with multiple data types without explicitly writing separate functions for each type. This feature makes our code more flexible and reusable.

<a name="understanding-type-parameters"></a>

2. Understanding Type Parameters 💡

In Go, type parameters are represented by a single capital letter. Type parameters are defined within angle brackets < > when used with functions or methods.

Quick Quiz
Question 1 of 1

What are type parameters in Go?

<a name="creating-generic-functions"></a>

3. Creating Generic Functions 💡

Let's create our first generic function that swaps two variables of any data type.

Example 1: Swap Function 📝

go
func Swap[T any](x, y T) (T, T) { temp := x x = y y = temp return x, y }

In the example above, we define a Swap function with a single type parameter T. This function can now be used with any data type. Here's how to use it with integers:

go
a := 5 b := 10 x, y := Swap(a, b) fmt.Println(x, y) // Output: 10 5

<a name="example-2-maximum-function"></a>

Example 2: Maximum Function 💡

Next, let's create a Maximum function that finds the maximum of two values of the same type:

go
func Maximum[T comparable](x, y T) T { if x > y { return x } return y }

You can use the Maximum function with integers and strings as follows:

go
a := 10 b := 5 c := "apple" d := "banana" fmt.Println(Maximum(a, b)) // Output: 10 fmt.Println(Maximum(c, d)) // Output: banana

<a name="using-type-parameters-in-methods"></a>

4. Using Type Parameters in Methods 💡

You can also use type parameters in methods. Let's create a generic struct Element that can hold elements of any data type.

Example: Generic Struct Method 📝

go
type Element[T any] struct { Value T } func (e Element[T]) Swap(other Element[T]) { e.Value, other.Value = other.Value, e.Value }

Now, we can create two Element instances and swap their values using the Swap method:

go
a := Element[int]{Value: 5} b := Element[int]{Value: 10} a.Swap(b) fmt.Println(a.Value, b.Value) // Output: 10 5

<a name="type-parameters-and-interfaces"></a>

5. Type Parameters and Interfaces 💡

Type parameters can be used with interfaces to create more flexible and reusable code. Let's create an interface Stack that has a Push and Pop method for any data type:

go
type Stack[T any] interface { Push(T) Pop() T }

You can create a simple IntStack implementation for integers:

go
type IntStack[T int] struct { data []T } func (s *IntStack[T]) Push(val T) { s.data = append(s.data, val) } func (s *IntStack[T]) Pop() T { if len(s.data) == 0 { panic("Stack is empty.") } val := s.data[len(s.data)-1] s.data = s.data[:len(s.data)-1] return val }

Now, you can use the IntStack with other types by implementing the Stack interface:

go
type StringStack[T string] struct { data []T } func (s *StringStack[T]) Push(val T) { s.data = append(s.data, val) } func (s *StringStack[T]) Pop() T { if len(s.data) == 0 { panic("Stack is empty.") } val := s.data[len(s.data)-1] s.data = s.data[:len(s.data)-1] return val } func main() { intStack := &IntStack[int]{} stringStack := &StringStack[string]{} intStack.Push(10) intStack.Push(20) fmt.Println(intStack.Pop()) // Output: 20 fmt.Println(intStack.Pop()) // Output: 10 stringStack.Push("apple") stringStack.Push("banana") fmt.Println(stringStack.Pop()) // Output: banana fmt.Println(stringStack.Pop()) // Output: apple }

In this example, we have created a Stack interface and implemented it for both integers and strings using type parameters. This allows us to use a single interface with different data types.

That's it for our Go Type Parameters lesson! You now have a solid understanding of how to use type parameters in functions, methods, and interfaces to create flexible and reusable code. Happy coding! 💻🎉