Go Type Tools

A library containing useful functions for working with Go types.

Table of Contents

  1. Reasoning
  2. Examples
    1. Array
    2. Map
    3. Int
    4. String
  3. Usage
  4. License
  5. Contributing
  6. Issues
  7. Changelog
  8. Future

Reasoning

One of the main reasons I wanted to create this library was becuause of the lack of functionality in the standard
library. Common things like filters, union, intersection, etc. are not available in the standard library. This library
is meant to be a collection of useful functions for working with Go types, improve readability and reduce boilerplate.

← Back to Table of Contents

Examples

Here are some examples of how/when to use this library.

Array

Filter

package main

func main() {
	// Create a new array of strings
	arr := []string{"a", "b", "c"}

	// Filter the array
	filtered := arraytools.Filter(arr, func(item string) bool {
		return item == "b"
	})

	// Print the filtered array
	fmt.Println(filtered)
}

Union

package main

func main() {
	// Create a new array of strings
	arr1 := []string{"a", "b", "c"}
	arr2 := []string{"c", "d", "e"}

	// Union the arrays
	union := arraytools.Union(arr1, arr2)

	// Print the union
	fmt.Println(union)
}

Map

Filter

package main

func main() {
	// Create a new map of strings
	m := map[string]string{
		"a": "1",
		"b": "2",
		"c": "3",
	}

	// Filter the map
	filtered := maptools.Filter(m, func(key string, value string) bool {
		return value == "2"
	})

	// Print the filtered map
	fmt.Println(filtered)
}

String

ToIntArray

package main

func main() {
	// Create a new string
	s := "2313,543,1"

	// To int array
	ints := stringtools.ToIntArray(s, ",")

	// Print the int array
	fmt.Println(ints)
}

Int

SecondsToDays

package main

func main() {
	// Create a new int
	i := inttools.SecondsToDays(86400)

	// Print the int
	fmt.Println(i)
}

← Back to Table of Contents

Usage

Import the package and use the functions you need.

import "github.com/eloonstra/go-type-tools/pkg/arraytools"
import "github.com/eloonstra/go-type-tools/pkg/maptools"
import "github.com/eloonstra/go-type-tools/pkg/stringtools"
import "github.com/eloonstra/go-type-tools/pkg/inttools"

← Back to Table of Contents

License

This project is licensed under the MIT license.

← Back to Table of Contents

Contributing

Contributions are welcome! Please open an issue or pull request if you have any suggestions.

← Back to Table of Contents

Issues

Currently, there are no known issues.

← Back to Table of Contents

Changelog

v0.0.1 (12-01-2022)

  • Initial release.

← Back to Table of Contents

Future

  • When Go 1.18 is released, implement generic functions for array and map tools.
  • Add MapBy functions to array, map, string, and int tools.
  • Add additional functions to string tools for converting to and from other types.

← Back to Table of Contents

GitHub

View Github