generic wrap for standard lib of golang.

Generic Std

generic wrap for standard lib of golang.

Generic will be supported in go 1.18.

But for now, standard lib will not support generic containers.

This lib wraps the standard lib. And add some useful generic functions or containers not implemented in standard lib.

The APIs between the lib and standard lib are not totally compatible. But I try to keep the different minimum.

See Also: Stream API for Go 1.18

Supported List

Wrapper for Standard lib

container/heap.Heap

container/list.List

container/ring.Ring

sort.Search

sort.Slice

sync/atomic.Value

sync.Map

sync.Pool

Addition utils

container/set.Set

Example

Without Generic

import (
    "sync"
)

func MapTest(){
    var m sync.Map
    m.Store("hello", "1")
    m.Store(2, 1)
    m.Store("Data", 2)
    data, ok := m.Load("hello") // data: interface{}
    if ok {
        realData := data.(string)
        ...
    }
    data2, ok := m.Load("Data") // data: interface{}
    if ok {
        realData := data2.(string) // panic, data2 is int ,can't assert to string
        ...
    }
}

This code will panic, but when you compile, you won't get any error info. If use generic, the thing will change. Type Error will be checked when compiling.

With Generic

import (
    "github.com/TobiasYin/generic_std/sync"
)

func MapTest(){
    var m sync.Map[string, string]
    m.Store("hello", "1")
    // m.Store(2, 1) // compile error
    // m.Store("Data", 2) // compile error
    m.Store("Data", "2")
    data, ok := m.Load("hello") // data: string, no need for type assertion
    if ok {
        // use data
        ...
    }
    data2, ok := m.Load("Data") // data: string
    if ok {
        // use data2
        // data2 type is string, this will be sure if code can compile. 
        ...
    }
}
Owner
Similar Resources

Golang errors with stacktrace and context

merry Add context to errors, including automatic stack capture, cause chains, HTTP status code, user messages, and arbitrary values. The package is la

Nov 19, 2022

Package semerr helps to work with errors in Golang.

Package semerr helps to work with errors in Golang.

semerr Package semerr helps to work with errors in Golang. Const error An error that can be defined as const. var errMutable error = errors.New("mutab

Oct 30, 2022

A Nostress Errors Package For Golang

A Nostress Errors Package For Golang

Nov 2, 2021

Just another error handling primitives for golang

errors Just another error handling primitives for golang Install go install github.com/WAY29/errors@latest Usage New error and print error context Th

Feb 19, 2022

Golang advanced error usage with stack tracing

UhOh Golang advanced error usage with stack tracing uhoh consists of 3 parts: Original error Description of error Stack trace File Function Line Usage

Mar 30, 2022

generic wrap for standard lib of golang.

Generic Std generic wrap for standard lib of golang. Generic will be supported in go 1.18. But for now, standard lib will not support generic containe

Mar 18, 2022

A standard way to wrap a proto message

Welcome to Pletter 👋 A standard way to wrap a proto message Pletter was born with a single mission: To standardize wrapping protocol buffer messages.

Nov 17, 2022

extension of SMx crypto support for go standard lib

Crypto Extension support of China crypto standards for go lib. You can simply copy and replace them to [your_go_src_path]/crypto Use as vendor is alte

Sep 21, 2022

Generic - Golang generic example

泛型 场景 假设需要写一个列表总数计算的函数,根据不同数据类型,我们可能分别要写 SumInts(data []int),SumFloats(data []fl

Jan 27, 2022

Provider-generic-workflows - A generic provider which uses argo workflows to define the backend actions.

provider-generic-workflows provider-generic-workflows is a generic provider which uses argo workflows for managing the external resource. This will re

Jan 1, 2022

Go-generic-unboxing - A quick ready to ship demo for go generic using the official example

Go generic This repo contain basic demo for installing and running go1.18beta1 v

Feb 1, 2022

This library implements the pub/sub pattern in a generic way. It uses Go's generic types to declare the type of the event.

observer This library implements the pub/sub pattern in a generic way. It uses Go's generic types to declare the type of the event. Usage go get githu

Nov 16, 2022

Go tool to wrap and fix errors with the new %w verb directive

Go tool to wrap and fix errors with the new %w verb directive

errwrap Wrap and fix Go errors with the new %w verb directive. This tool analyzes fmt.Errorf() calls and reports calls that contain a verb directive t

Nov 10, 2022

Go tool to wrap and fix errors with the new %w verb directive

Go tool to wrap and fix errors with the new %w verb directive

errwrap Wrap and fix Go errors with the new %w verb directive. This tool analyzes fmt.Errorf() calls and reports calls that contain a verb directive t

Nov 10, 2022

ZipExec is a Proof-of-Concept (POC) tool to wrap binary-based tools into a password-protected zip file.

ZipExec is a Proof-of-Concept (POC) tool to wrap binary-based tools into a password-protected zip file.

ZipExec ZipExec is a Proof-of-Concept (POC) tool to wrap binary-based tools into a password-protected zip file. This zip file is then base64 encoded i

Dec 31, 2022

Wrap zap for easy using.

go-project-pkg/log Wrap zap for easy using. Installation $ go get -u github.com/go-project-pkg/log Usage Use default logger: import "github.com/go-pro

Nov 8, 2021

Wrap unicode text not to exceed a certain width.

wwrap Wrap unicode text not to exceed a specified column width. There is a fold utility in the GNU Coreutils package, but unfortunately it works on by

Dec 1, 2021

🔐 Wrap keys from HSM using CKM_RSA_AES_KEY_WRAP step by step

🔐 pkcs11-key-wrap Wrap keys from HSM using CKM_RSA_AES_KEY_WRAP step by step. This tool can be used for example for exporting keys from Amazon's Clou

Aug 15, 2022

Wrap byte read options with uniform interface for io.Reader and byte slice

nibbler Nibble chunks from Reader streams and slice in a common way Overview This is a golang module that provides an interface for treating a Reader

Dec 23, 2021
🔐 Wrap keys from HSM using CKM_RSA_AES_KEY_WRAP step by step

?? pkcs11-key-wrap Wrap keys from HSM using CKM_RSA_AES_KEY_WRAP step by step. This tool can be used for example for exporting keys from Amazon's Clou

Aug 15, 2022
Wrap contains a method for wrapping one Go error with another.

Note: this code is still in alpha stage. It works but it may change subtly in the near future, depending on what comes out of golang/go#52607. Wrap.Wi

Jun 27, 2022
Errors - A lib for handling error gracefully in Go

?? Errors Errors 是一个用于优雅地处理 Go 中错误的库。 Read me in English ??‍ 功能特性 优雅地处理 error,嗯,

Jan 17, 2022
Drop-in replacement for the standard library errors package and github.com/pkg/errors

Emperror: Errors Drop-in replacement for the standard library errors package and github.com/pkg/errors. This is a single, lightweight library merging

Dec 20, 2022
Fault injection library in Go using standard http middleware

Fault The fault package provides go http middleware that makes it easy to inject faults into your service. Use the fault package to reject incoming re

Dec 25, 2022
Generic error handling with panic, recover, and defer.

Generic error handling with panic, recover, and defer.

Aug 25, 2022
A Go (golang) package for representing a list of errors as a single error.

go-multierror go-multierror is a package for Go that provides a mechanism for representing a list of error values as a single error. This allows a fun

Jan 1, 2023
Golang errors with stack trace and source fragments.
Golang errors with stack trace and source fragments.

Golang Errors with Stack Trace and Source Fragments Tired of uninformative error output? Probably this will be more convenient: Example package main

Dec 17, 2022
Hierarchical errors reporting done right in Golang

Hierarchical errors made right Hate seeing error: exit status 128 in the output of programs without actual explanation what is going wrong? Or, maybe,

Nov 9, 2021
Crash your app in style (Golang)
Crash your app in style (Golang)

panicparse Parses panic stack traces, densifies and deduplicates goroutines with similar stack traces. Helps debugging crashes and deadlocks in heavil

Jan 5, 2023