pie
🍕 Enjoy a slice! A utility library for dealing with slices and maps that focuses on type safety and performance.
Top Related Projects
Quick Overview
Pie is a Go library that provides a simple and intuitive way to create and manipulate pie charts. It offers a range of customization options and supports various data formats, making it a versatile tool for data visualization.
Pros
- Simplicity: Pie provides a straightforward API for creating and customizing pie charts, making it easy to integrate into your Go projects.
- Customization: The library offers a wide range of options for customizing the appearance of the pie charts, including colors, labels, and legends.
- Data Flexibility: Pie supports various data formats, including slices, maps, and structs, allowing you to easily visualize your data.
- Lightweight: The library is lightweight and has no external dependencies, making it easy to include in your projects.
Cons
- Limited Functionality: Pie is primarily focused on creating pie charts and may not offer the same level of functionality as more comprehensive data visualization libraries.
- Lack of Advanced Features: The library does not currently provide advanced features such as interactive charts or support for other chart types.
- Limited Documentation: The project's documentation could be more comprehensive, which may make it challenging for new users to get started.
- Potential Performance Issues: Depending on the size and complexity of your data, the library may not perform as well as more optimized data visualization solutions.
Code Examples
Here are a few examples of how to use the Pie library:
- Creating a Basic Pie Chart:
package main
import (
"github.com/elliotchance/pie"
"fmt"
)
func main() {
data := []float64{10, 20, 30, 40}
chart := pie.New(data)
fmt.Println(chart.Render())
}
- Customizing the Pie Chart:
package main
import (
"github.com/elliotchance/pie"
"fmt"
)
func main() {
data := []float64{10, 20, 30, 40}
chart := pie.New(data)
chart.Title = "My Pie Chart"
chart.Colors = []string{"#FF0000", "#00FF00", "#0000FF", "#FFFF00"}
chart.Labels = []string{"Slice 1", "Slice 2", "Slice 3", "Slice 4"}
fmt.Println(chart.Render())
}
- Using Structs as Data Source:
package main
import (
"github.com/elliotchance/pie"
"fmt"
)
type Data struct {
Name string
Value float64
}
func main() {
data := []Data{
{"Slice 1", 10},
{"Slice 2", 20},
{"Slice 3", 30},
{"Slice 4", 40},
}
chart := pie.New(data)
fmt.Println(chart.Render())
}
Getting Started
To get started with the Pie library, follow these steps:
- Install the library using Go's package manager:
go get github.com/elliotchance/pie
- Import the library in your Go file:
import "github.com/elliotchance/pie"
- Create a new pie chart using the
pie.New()
function, passing in your data:
data := []float64{10, 20, 30, 40}
chart := pie.New(data)
- Customize the chart by setting properties like the title, colors, and labels:
chart.Title = "My Pie Chart"
chart.Colors = []string{"#FF0000", "#00FF00", "#0000FF", "#FFFF00"}
chart.Labels = []string{"Slice 1", "Slice 2", "Slice 3", "Slice 4"}
- Render the chart and print the output:
fmt.Println(chart.Render())
That's it! You've now created a basic pie chart using the Pie library.
Competitor Comparisons
A toolkit with common assertions and mocks that plays nicely with the standard library
Pros of testify
- More comprehensive testing toolkit with assertions, mocking, and suite support
- Wider adoption and community support
- Better integration with Go's built-in testing package
Cons of testify
- Larger codebase and more dependencies
- Steeper learning curve for beginners
- May introduce unnecessary complexity for simple test cases
Code Comparison
testify:
assert := assert.New(t)
assert.Equal(expected, actual, "they should be equal")
assert.NotNil(object)
mock := new(MyMockedObject)
mock.On("DoSomething", 123).Return(true, nil)
pie:
pie.Equal(t, expected, actual)
pie.NotNil(t, object)
Summary
testify offers a more feature-rich testing experience with assertions, mocking, and suite support, making it suitable for complex testing scenarios. It has wider adoption and better integration with Go's testing package. However, it comes with a larger codebase and steeper learning curve.
pie, on the other hand, provides a simpler and more lightweight approach to testing. It focuses primarily on assertions and has a smaller footprint. While it may lack some advanced features, it can be easier to learn and use for basic testing needs.
The choice between the two depends on the project's complexity and testing requirements. testify is better suited for larger projects with diverse testing needs, while pie may be preferable for smaller projects or those seeking a minimalist testing approach.
Ginkgo's Preferred Matcher Library
Pros of Gomega
- More comprehensive assertion library with a wide range of matchers
- Better integration with Ginkgo testing framework
- Supports asynchronous testing and eventually consistent assertions
Cons of Gomega
- Steeper learning curve due to more complex API
- Requires additional setup and configuration compared to Pie
- May be overkill for simple testing scenarios
Code Comparison
Gomega:
Expect(someValue).To(Equal(42))
Expect(someSlice).To(ContainElement("foo"))
Expect(someFunction).To(Panic())
Pie:
assert.Equal(t, 42, someValue)
assert.Contains(t, someSlice, "foo")
assert.Panics(t, someFunction)
Summary
Gomega is a more feature-rich and powerful assertion library, particularly well-suited for complex testing scenarios and integration with the Ginkgo framework. It offers advanced features like asynchronous testing and custom matchers.
Pie, on the other hand, provides a simpler and more straightforward approach to assertions. It's easier to get started with and may be sufficient for basic testing needs.
The choice between the two depends on the complexity of your testing requirements and your preference for API style. Gomega offers more flexibility and power, while Pie focuses on simplicity and ease of use.
Go testing in the browser. Integrates with `go test`. Write behavioral tests in Go.
Pros of GoConvey
- Provides a web UI for real-time test results and coverage reports
- Offers a more comprehensive testing framework with built-in assertions and matchers
- Supports automatic test running on file changes
Cons of GoConvey
- Steeper learning curve due to its more complex API and features
- May be considered overkill for simple projects or small codebases
- Requires additional setup and configuration compared to Pie
Code Comparison
GoConvey:
Convey("Given a user", t, func() {
user := NewUser("John", 25)
Convey("When checking age", func() {
So(user.Age, ShouldBeGreaterThan, 18)
})
})
Pie:
assert.Equal(t, 25, user.Age)
assert.True(t, user.Age > 18)
GoConvey offers a more expressive and hierarchical way of structuring tests, while Pie focuses on simple, straightforward assertions. GoConvey's syntax allows for nested contexts and more descriptive test scenarios, whereas Pie provides a more traditional approach to testing with individual assertion statements.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
ð github.com/elliotchance/pie
Enjoy a slice! pie
is a library of utility functions for common operations
on slices and maps.
Quick Start
If you are using (or require) Go 1.17 or below, you will have to use v1.
pie
can be used in two ways, the first is to use the regular
parameterized functions:
package main
import (
"fmt"
"strings"
"github.com/elliotchance/pie/v2"
)
func main() {
names := pie.FilterNot([]string{"Bob", "Sally", "John", "Jane"},
func(name string) bool {
return strings.HasPrefix(name, "J")
})
fmt.Println(names) // "[Bob Sally]"
}
Or, if you need to chain multiple operations you can use one of:
pie.Of
- works with any element type, but functions are limited.pie.OfOrdered
- only works with numbers and strings, but has more functions.pie.OfNumeric
- only works with numbers, but has all functions.
package main
import (
"fmt"
"strings"
"github.com/elliotchance/pie/v2"
)
func main() {
name := pie.Of([]string{"Bob", "Sally", "John", "Jane"}).
FilterNot(func(name string) bool {
return strings.HasPrefix(name, "J")
}).
Map(strings.ToUpper).
Last()
fmt.Println(name) // "SALLY"
}
You can find the full documentation here.
FAQ
What are the requirements?
pie
v2 only supports Go 1.18+. If you have an older version you can
use v1.
What are the goals of pie
?
-
Type safety. I never want to hit runtime bugs because I could pass in the wrong type, or perform an invalid type case out the other end.
-
Performance. The functions need to be as fast as native Go implementations otherwise there's no point in this library existing.
-
Nil-safe. All of the functions will happily accept nil and treat them as empty slices. Apart from less possible panics, it makes it easier to chain.
-
Immutable. Functions never modify inputs (except in cases where it would be illogical), unlike some built-ins such as
sort.Strings
.
How do I contribute a function?
Pull requests are always welcome.
Here is a comprehensive list of steps to follow to add a new function:
-
Create a new file for your function (tip: copy an existing file can be quicker). Add your implmentation and comment.
-
Create appropriate tests.
-
If your function accepts a slice, it should also be added to the
OfSlice
API (seeof.go
).
Why is the emoji a slice of pizza instead of a pie?
I wanted to pick a name for the project that was short and had an associated emoji. I liked pie, but then I found out that the pie emoji is not fully supported everywhere. I didn't want to change the name of the project to cake, but pizza pie still made sense. I'm not sure if I will change it back to a pie later.
Top Related Projects
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot