Topic Overview and Importance

Hello and welcome! Today, we're diving into practical data manipulation techniques in Go. We'll be using Go's slices and maps to represent our data and perform projection, filtering, and aggregation. The operations will be encapsulated within a Go struct, ensuring our code remains organized and efficient. So let's get ready to tackle data manipulation in a clean, idiomatic Go manner!

Introduction to Data Manipulation

Data manipulation can be likened to sculpting — it's all about shaping and conditioning data to fit our specific needs. In Go, slices and maps offer powerful ways to handle and transform data. We'll harness these constructs within a Go struct to provide a tidy toolbox for our data operations. Here's a simple Go struct, DataStream, that will serve as the framework for our exploration:

package main

import "fmt"

type DataStream struct {
    data []map[string]string
}

func NewDataStream(data []map[string]string) *DataStream {
    return &DataStream{data: data}
}

func (ds *DataStream) PrintData() {
    for _, entry := range ds.data {
        for key, value := range entry {
            fmt.Printf("%s: %s, ", key, value)
        }
        fmt.Println()
    }
}
Data Projection in Practice

Our first task is data projection — selecting specific attributes of interest. Let's say we're handling a dataset about individuals, and we're only interested in names and ages. We can extend our DataStream struct with a Project method to efficiently handle this:

// ... previous code

func (ds *DataStream) Project(projectFunc func(map[string]string) map[string]string) *DataStream {
    var projectedData []map[string]string
    for _, entry := range ds.data {
        projectedData = append(projectedData, projectFunc(entry))
    }
    return NewDataStream(projectedData)
}

func main() {
    ds := NewDataStream([]map[string]string{
        {"name": "Alice", "age": "25", "profession": "Engineer"},
        {"name": "Bob", "age": "30", "profession": "Doctor"},
    })

    projectedDs := ds.Project(func(entry map[string]string) map[string]string {
        return map[string]string{"name": entry["name"], "age": entry["age"]}
    })
}

In this example, by applying the Project method we filter our dataset to focus solely on names and ages.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal