so71677581

package
v0.0.0-...-24ca9bf Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2022 License: MIT, MIT Imports: 2 Imported by: 0

README

SO71677581

Tricky and fun utilities for Go programs.


GitHub Workflow Status Codecov

Contributor Covenant

Twitter Follow GitHub followers


Getting Started

Prerequisites

Developed with $( go version; ). Go is extremely backwards compatible and semver stable. Nearly any v1.x should work fine.


Installation

To use this repo as a template for your own project:

gh repo create -y --public --template "https://github.com/skeptycal/SO71677581"

Clone this repo to test and contribute:

# add repo to $GOPATH (xxxxxx is your computer login username)
go get github.com/xxxxxx/SO71677581

cd ${GOPATH}/src/github.com/xxxxxx/SO71677581

# test results and coverage info
./go.test.sh

# install as a utility package
go install

Use the Issues and PR templates on the GitHub repo page to contribute.


Basic Usage

This is a copy of the example script available in the cmd/example/SO71677581 folder:

package main

import "github.com/skeptycal/SO71677581"

func main() {
    SO71677581.Example()
}

To try it out:

# change to the sample folder
cd cmd/example/SO71677581

# run the main.go program
go run ./main.go

# to compile as an executable
go build

Code of Conduct and Contributing

Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us. Please read the Code of Conduct for details before submitting anything.


Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.


Contributors and Inspiration

  • Michael Treanor (GitHub / Twitter) - Initial work, updates, maintainer
  • Francesc Campoy - Inspiration and great YouTube videos!

See also the list of contributors who participated in this project.


License

Licensed under the MIT https://opensource.org/licenses/MIT - see the LICENSE file for details.

Documentation

Overview

package main contains a Response to Stack Overflow question 71677581

Stack Overflow Post: https://stackoverflow.com/q/71677581

Go Playground version: https://go.dev/play/p/jR5MpX-Fopc

This repo: https://

MIT License

GitHub: https://

Twitter: https://twitter.com/skeptycal

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MyFuncAny

func MyFuncAny(ex any) any

func MyFuncInt

func MyFuncInt(ex int) int

func PrintFExample

func PrintFExample(m ExampleFuncMap)

func PrintMapExample

func PrintMapExample[K comparable, V any](m map[K]V)

PrintIExample actually can print any map, but is used here for the example type IExampleMap.

Types

type AnyMap

type AnyMap[K comparable, V any] map[K]V

AnyMap is an extremely generic definition, the map type can be defined in any possible combination of key and value types, so long as the 'key' type is comparable.

type ExampleFunc

type ExampleFunc[T any] func(ex T) T

type based on function signature instead of interface implementation

type ExampleFuncKeyMap

type ExampleFuncKeyMap[K comparable, V any] AnyMap[K, FuncMap[V]]

example of a function map with multiple types of keys and function argument allowed by the constraints

type ExampleFuncMap

type ExampleFuncMap FuncMap[any]

final type instantiated with 'any'

func CreateSampleFunctionMap

func CreateSampleFunctionMap() ExampleFuncMap

CreateSampleFunctionMap returns an example map with several sample values.

type Exampler

type Exampler[T any] interface {
	ExampleFunc(ex T) T
}

It is getting somewhat cluttered ... perhaps it is more clear to define the interface first ...

type ExamplerMap

type ExamplerMap[T any] StringMap[Funcer[T]]

This is probably the most clear and concise definition for the original map type: string keys, Exampler values, any type may be instantiated in the Exampler

type FloatType

type FloatType interface{ float32 | float64 }

some useful constraints - incomplete, but good for example (e.g. leaves out complex numbers and uintptr, as well as ignoring structs, sequences, and sets that may be comparable based on their fields) as of this writing (4/01/22), the standard library 'constraints' package containing most of these constraints appears to be unavailable

type FuncMap

type FuncMap[T any] StringMap[ExampleFunc[T]]

using a function signature as the value

type Funcer

type Funcer[T any] func(ex T) T

types based on the original interface-based type

type IExample

type IExample[T any] interface {
	ExampleFunc(ex T) T
}

The example in the question used a map with strings for keys and the IExample interface as the value type and the plan for the map was:

mapping := map[string]IExample[any]{
     // .......
}

type IExampleBool

type IExampleBool = ExamplerMap[bool]

types based on the original interface-based type

type IExampleFloat

type IExampleFloat = ExamplerMap[float64]

a few other ExamplerMaps instantiated for various types as examples

type IExampleMap

type IExampleMap[T any] map[string]interface{ ExampleFunc(ex T) T }

IExampleMap is a type I created for the entire map, instead of just the 'value' used in the original example. But ... is this the way to go? Is this clear and does it communicate the functionality?

type IExampleMapAny

type IExampleMapAny = IExampleMap[any]

examples of IExampleMap instantiated for various types

func CreateSampleInterfaceMap

func CreateSampleInterfaceMap() IExampleMapAny

CreateSampleInterfaceMap returns an example interface map with several sample values.

type IExampleMapInt

type IExampleMapInt = IExampleMap[int]

types based on the original interface-based type

type IExampleMapTests

type IExampleMapTests = IExampleMap[struct {
	name    string
	fn      interface{}
	in      []interface{}
	want    []interface{}
	wantErr bool
}]

types based on the original interface-based type

type IntMap

type IntMap[V any] AnyMap[string, V]

IntMap is a generic map with int keys

type IntType

type IntType interface {
	int | int8 | int16 | int32 | int64
}

some useful constraints - incomplete, but good for example (e.g. leaves out complex numbers and uintptr, as well as ignoring structs, sequences, and sets that may be comparable based on their fields) as of this writing (4/01/22), the standard library 'constraints' package containing most of these constraints appears to be unavailable

type Number

type Number interface{ IntType | UintType | FloatType }

some useful constraints - incomplete, but good for example (e.g. leaves out complex numbers and uintptr, as well as ignoring structs, sequences, and sets that may be comparable based on their fields) as of this writing (4/01/22), the standard library 'constraints' package containing most of these constraints appears to be unavailable

type Ordered

type Ordered interface{ Number | ~string }

some useful constraints - incomplete, but good for example (e.g. leaves out complex numbers and uintptr, as well as ignoring structs, sequences, and sets that may be comparable based on their fields) as of this writing (4/01/22), the standard library 'constraints' package containing most of these constraints appears to be unavailable

type StringMap

type StringMap[V any] AnyMap[string, V]

StringMap is a generic map with string keys.

type Test

type Test[O Ordered] struct {
	// contains filtered or unexported fields
}

Perhaps ... a useful set of defintions for running generic table-based tests.

type TestMap

type TestMap[K comparable, V Ordered] AnyMap[K, Test[V]]

TestMap is a map of table-based test objects

type TestRunner

type TestRunner[O Ordered] func(t *testing.T, in ...any) (got O)

Perhaps ... a useful set of defintions for running generic table-based tests.

type UintType

type UintType interface {
	uint | uint8 | uint16 | uint32 | uint64
}

some useful constraints - incomplete, but good for example (e.g. leaves out complex numbers and uintptr, as well as ignoring structs, sequences, and sets that may be comparable based on their fields) as of this writing (4/01/22), the standard library 'constraints' package containing most of these constraints appears to be unavailable

Jump to

Keyboard shortcuts

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