slices

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 11, 2018 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package slices contains some slice related helper functions.

Example
package main

import (
	"fmt"

	"github.com/Akagi201/utilgo/slices"
)

type Tag struct {
	Name string
}

type Post struct {
	Title string
	Tags  []*Tag
}

func main() {
	post := &Post{
		Title: "GOLANG",
		Tags: []*Tag{
			{"Go"}, {"Golang"}, {"Gopher"},
		},
	}
	s, _ := slices.ToStrings(post.Tags, "Name")
	fmt.Println(s)

}
Output:

[Go Golang Gopher]

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotSlice happens when the value passed is not a slice.
	ErrNotSlice = errors.New("not slice")

	// ErrNotString happens when the value of the field is not a string.
	ErrNotString = errors.New("not string")

	// ErrNotInt happens when the value of the field is not an int.
	ErrNotInt = errors.New("not int")

	// ErrNotFloat happens when the value of the field is not a float64.
	ErrNotFloat = errors.New("not float64")

	// ErrNotBool happens when the value of the field is not a bool.
	ErrNotBool = errors.New("not bool")
)

Functions

func IndexOf

func IndexOf(val interface{}, slice interface{}) int

IndexOf returns the first index at which a given element can be found in the slice, or -1 if it is not present. The second argument must be a slice or array.

Example
package main

import (
	"fmt"

	"github.com/Akagi201/utilgo/slices"
)

func main() {

	s := []string{"a1", "a2", "aa"}

	{
		v := "aa"
		i := slices.IndexOf(v, s)

		fmt.Println(i)
	}

	{
		v := "bb"
		i := slices.IndexOf(v, s)

		fmt.Println(i)
	}

}
Output:

2
-1

func MaxFloat

func MaxFloat(slice []float64) float64

MaxFloat max float of the slice

func MaxInt

func MaxInt(slice []int) int

MaxInt max int of the slice

func MinFloat

func MinFloat(slice []float64) float64

MinFloat min float of the slice

func MinInt

func MinInt(slice []int) int

MinInt min int of the slice

func SumFloat

func SumFloat(slice []float64) float64

func SumInt

func SumInt(slice []int) int

func ToBools

func ToBools(slice interface{}, fieldName string) (s []bool, err error)

ToBools maps a field to a slice of bool.

func ToBoolsUnsafe

func ToBoolsUnsafe(slice interface{}, fieldName string) []bool

ToBoolsUnsafe maps a field to a slice of bool but not returns an error. If an error still happens, s will be nil.

func ToFloats

func ToFloats(slice interface{}, fieldName string) (s []float64, err error)

ToFloats maps a field to a slice of int.

func ToFloatsUnsafe

func ToFloatsUnsafe(slice interface{}, fieldName string) []float64

ToFloatsUnsafe maps a field to a slice of float but not returns an error. If an error still happens, s will be nil.

func ToInts

func ToInts(slice interface{}, fieldName string) (s []int, err error)

ToInts maps a field to a slice of int.

func ToIntsUnsafe

func ToIntsUnsafe(slice interface{}, fieldName string) []int

ToIntsUnsafe maps a field to a slice of int but not returns an error. If an error still happens, s will be nil.

func ToStrings

func ToStrings(slice interface{}, fieldName string) (s []string, err error)

ToStrings maps a field to a slice of string.

func ToStringsUnsafe

func ToStringsUnsafe(slice interface{}, fieldName string) []string

ToStringsUnsafe maps a field to a slice of string but not returns an error. If an error still happens, s will be nil.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL