Category: Go

  • How to use generics in structs and interfaces in Golang?

    How to use generics in structs and interfaces in Golang?

    Golang 1.18 introduced support for generics, allowing developers to write code that is independent of specific types. This means that functions and types can now be written to work with any set of types. In this article, we’ll explore how to use Golang generics in a struct and with interfaces.

    Generic function to handle multiple types

    Type parameters in programming have constraints that define which types they can accept. These constraints serve as guidelines. During compilation, the actual type provided must comply with the constraint; otherwise, you’ll get a compilation error.

    func IsGreater[V float64 | int](a, b V) bool {
    	return a > b
    }

    Your generic code can only perform operations that are supported by the type parameter’s constraints. Attempting string operations on a numeric-only type parameter will cause a compilation error.

    Using generics in a struct

    type Model[T any] struct {
    	Data []T
    }

    A generic type must be instantiated when used, and instantiation requires a type parameter list. Here’s an example:

    func main() {
    	// passing int as type parameter
    	modelInt := Model[int]{Data: []int{1, 2, 3}}
    	fmt.Println(modelInt.Data) // [1 2 3]
    	// passing string as type parameter
    	modelStr := Model[string]{Data: []string{"a", "b", "c"}}
    	fmt.Println(modelStr.Data) // [a b c]
    }

    Define methods on generic types

    If you declare methods on a generic type, you must repeat the type parameter declaration on the receiver, even if the type parameters are not used in the method scope. For example:

    type Model[T any] struct {
    	Data []T
    }
    
    func (m *Model[T]) Push(item T) {
    	m.Data = append(m.Data, item)
    }

    Using interfaces with Golang Generics

    Types don’t actually implement generic interfaces, they implement instantiations of generic interfaces. You can’t use a generic type (including interfaces) without instantiation.

    Here’s an example:

    type Getter[T any] interface {
    	Get() T
    }
    
    type Model[T any] struct {
    	Data []T
    } 
    
    // implements Getter
    func (m *Model[T]) Get(i int) T {
    	return m.Data[i]
    }

    Full example

    Declare a type constraint as interface

    In the next section, we’ll extract the previously defined constraint into its own interface for reuse in multiple places. Using interfaces as type constraints allows any implementing type to satisfy the constraint. For instance, if an interface has three methods and is used as a type parameter in a generic function, all type arguments must have those methods. This section also explores constraint interfaces that refer to specific types. Example:

    type Number interface {
    	int | float64
    }
    type Getter[T Number] interface {
    	Get() T
    }
    type Model[T Number] struct {
    	Data []T
    }
    
    func (m *Model[T]) Push(item T) {
    	m.Data = append(m.Data, item)
    }
    
    func (m *Model[T]) Get(i int) T {
    	return m.Data[i]
    }
    
    func main() {
    	// passing int as type parameter
    	modelInt := Model[int]{Data: []int{1, 2, 3}}
    	fmt.Println(modelInt.Data) // [1 2 3] // passing float64 as type parameter
    	modelFloat := Model[float64]{Data: []float64{1.1, 2.2, 0.02}}
    	fmt.Println(modelFloat.Data) // [1.1 2.2 0.02] modelInt.Push(4)
    	fmt.Println(modelInt.Data)   // [1 2 3 4] itemAtOne := modelFloat.Get(1)
    	fmt.Println(itemAtOne)       // 2.2
    }

    In this example, if we try to use a type that does not satisfy the Number interface, you will get a compile error.

    _ = Model[string]{Data: []string{"a", "b"}}
    // string does not satisfy Number (string missing in int | float64)

    Full example

    Express ‘Family of Types’ constraints

    To express a constraint covering all types with an underlying type we can use the `~` operator. Here is an example from the slices package Clip function:

    func Clip[S ~[]E, E any](s S) S {
    	return s[:len(s):len(s)]
    }

    Essentially, this constraint ensures that S can only be a slice that can hold elements of any data type. This allows the Clip function to be generic and work with various slice types like []int, []string, or even slices of custom structs.

    Here is a more straightforward example:

    func Hello[S ~string](s S) S {
    	return fmt.Sprintf("Hello, %s", s)
    }

    Full example

    Return zero (default) values

    The *new(T) idiom

    The preferred option, as suggested in golang-nuts, involves using new(T). While potentially less readable, this approach simplifies finding and replacing instances if a zero-value builtin is introduced in the language. It also allows for concise one-line assignments.

    The new builtin function allocates memory for a variable of any type T and returns a pointer to it. Dereferencing the result (*new(T)) effectively yields the zero value for the type T. This approach also works with type parameters.

    func Zero[T any]() T {
        return *new(T)
    }

    var of type T

    Straightforward and easier to read, though it always requires one line more:

    func Zero[T any]() T {
      var zero T
      return zero
    }

    Named return types

    You can use named returns to avoid explicitly declaring variables. While not everyone loves this style, it can be helpful in complex functions or when using defer statements. Here’s a simple example:

    func Zero[T any]() (ret T) {
      return
    }

    Full example

    In conclusion, Golang generics provide developers with the flexibility to write code that is independent of specific types. This allows for more reusable and maintainable code.

  • Understanding Interfaces in Go

    Introduction

    In this tutorial, we will dive into interfaces in Go, which are powerful tools for grouping types based on shared behaviors. By understanding interfaces, you can write cleaner, more flexible, and more maintainable code.

    What Are Interfaces?

    An interface in Go defines a set of method signatures. Any type that implements these methods automatically satisfies the interface. This allows you to treat different types uniformly when they share common behaviors.


    Example Scenario

    To explain interfaces, we’ll create a simple program that works with shapes like squares and circles. Each shape will have methods to calculate its area and circumference.

    Here’s a breakdown:

    • Square: Defined by a length property.
    • Circle: Defined by a radius property.
    • Both shapes will implement methods Area() and Circumference() that return float64 values.

    Step 1: Define Shape Types

    package main
    
    import (
    	"math"
    )
    
    type Square struct {
    	Length float64
    }
    
    func (s Square) Area() float64 {
    	return s.Length * s.Length
    }
    
    func (s Square) Circumference() float64 {
    	return 4 * s.Length
    }
    
    type Circle struct {
    	Radius float64
    }
    
    func (c Circle) Area() float64 {
    	return math.Pi * c.Radius * c.Radius
    }
    
    func (c Circle) Circumference() float64 {
    	return 2 * math.Pi * c.Radius
    }
    

    Step 2: Define the Shape Interface

    An interface groups types based on methods they implement. Here, the Shape interface requires both an Area() and a Circumference() method.

    type Shape interface {
    	Area() float64
    	Circumference() float64
    }

    Any type that implements these methods will automatically be of type Shape.


    Step 3: Write a Function to Print Shape Information

    We can create a single function that works for both squares and circles by using the Shape interface.

    func PrintShapeInfo(shape Shape) {
    	fmt.Printf("Area: %.2f\n", shape.Area())
    	fmt.Printf("Circumference: %.2f\n", shape.Circumference())
    }

    This function can accept any type that satisfies the Shape interface.


    Step 4: Use the Interface in the Main Function

    Create a slice of shapes and print their information.

    func main() {
    	shapes := []Shape{
    		Square{Length: 5},
    		Circle{Radius: 3},
    	}
    
    	for _, shape := range shapes {
    		PrintShapeInfo(shape)
    		fmt.Println("---") // Separator between shapes
    	}
    }

    Running the Code

    To run the program, use the following command:

    go run main.go

    You should see output similar to:

    Area: 25.00
    Circumference: 20.00
    ---
    Area: 28.27
    Circumference: 18.85
    ---

    Key Takeaways

    • Interfaces group types: They allow you to define behavior without specifying how types should implement it.
    • Automatic satisfaction: If a type implements all methods of an interface, it automatically satisfies that interface.
    • Flexible functions: Interfaces enable you to write general functions that work for different types.

    Full code


    Conclusion

    By leveraging interfaces, your Go programs can be more modular and easier to extend. This tutorial has shown how to define interfaces, implement them, and use them to create flexible functions.

    Happy coding!