to

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2022 License: Apache-2.0 Imports: 5 Imported by: 25

README

Library of Converters in Go 1.18+

Go Version GoDoc License Go Report Card

Documentation

Overview

Package to contains a number of converters that take any number of types and return something transformed from them. It also contains a more granular approach to fmt.Stringer.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func FuncName added in v0.1.1

func FuncName(i any) string

FuncName makes a best effort attempt to return the string name of the passed function. Anonymous functions are named "funcN" where N is the order of appearance within the current scope. Note that this function will panic if not passed a function.

Example
package main

import (
	"github.com/rwxrob/fn"
	"github.com/rwxrob/fn/each"
	"github.com/rwxrob/to"
)

func Foo() {}

func main() {

	f1 := func() {}
	f2 := func() {}

	// Foo is defined outside of the ExampleFuncName

	each.Println(fn.Map([]any{f1, f2, Foo, to.Lines}, to.FuncName))

}
Output:
func1
func2
Foo
Lines

func Lines added in v0.1.1

func Lines(in any) []string

Lines transforms the input into a string and then divides that string up into lines (\r?\n) suitable for functional map operations.

Example
package main

import (
	"github.com/rwxrob/fn/each"
	"github.com/rwxrob/to"
)

func main() {
	buf := `
some

thing 
here

mkay
`
	each.Print(to.Lines(buf))
}
Output:
something heremkay

func String

func String(in any) string

String converts whatever is passed to its fmt.Sprintf("%v") string version (but avoids calling it if possible). Be sure you use things with consistent string representations.

Example
package main

import (
	"fmt"

	"github.com/rwxrob/to"
)

type stringer struct{}

func (s stringer) String() string { return "stringer" }

func main() {
	stuff := []any{
		"some", []byte{'t', 'h', 'i', 'n', 'g'},
		1, 2.234, stringer{},
	}
	for _, s := range stuff {
		fmt.Printf("%q ", to.String(s))
	}
}
Output:
"some" "thing" "1" "2.234" "stringer"

Types

type Stringer added in v0.1.1

type Stringer interface {
	String() string
}

Stringer interfaces fulfills fmt.Stringer with the additional promise that the output of String method will always be both consistently parsable (say as JSON) and will never span more than a single line.

type StringerLong added in v0.1.1

type StringerLong interface {
	StringLong() string
}

StringerLong specifies an addition to the fmt.Stringer (String) method that will produce a readable, parsable representation that may span multiple lines.

Jump to

Keyboard shortcuts

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