Golang: unify nil and empty slices and maps

unifynil, unify nil and empty slices and maps in Golang

Empty slices and maps can be nil or not nil in Go. It may become a nightmare in tests and JSON processing.

This package provides a simple way to convert all empty slices or maps to nil or non-nil versions.

Example

To replace all nil slices and maps to not-nil:

import "github.com/starius/unifynil"

type Response struct {
    Items []int             `json:"items"`
    Users map[string]string `json:"users"`
}

res := &Response{} // Leave the slice and the map nil.

buf, _ := json.Marshal(res)
fmt.Println(string(buf))
// {"items": null, "users": null}

unifynil.Unify(res, unifynil.SliceToEmpty(), unifynil.MapToEmpty())

// Now res.Items = []int{} and res.Users = map[string]string{}.

buf, _ = json.Marshal(res)
fmt.Println(string(buf))
// {"items": [], "users": {}}

Convert back to nils:

unifynil.Unify(res, unifynil.SliceToNil(), unifynil.MapToNil())

// Now res.Items = nil and res.Users = nil.

buf, _ = json.Marshal(res)
fmt.Println(string(buf))
// {"items": null, "users": null}
Owner
Boris Nagaev
Developer
Boris Nagaev
Similar Resources

Code Generation for Functional Programming, Concurrency and Generics in Golang

goderive goderive derives mundane golang functions that you do not want to maintain and keeps them up to date. It does this by parsing your go code fo

Dec 25, 2022

Copier for golang, copy value from struct to struct and more

Copier I am a copier, I copy everything from one to another Features Copy from field to field with same name Copy from method to field with same name

Jan 8, 2023

Extremely flexible golang deep comparison, extends the go testing package, tests HTTP APIs and provides tests suite

Extremely flexible golang deep comparison, extends the go testing package, tests HTTP APIs and provides tests suite

go-testdeep Extremely flexible golang deep comparison, extends the go testing package. Latest news Synopsis Description Installation Functions Availab

Jan 5, 2023

A well tested and comprehensive Golang statistics library package with no dependencies.

Stats - Golang Statistics Package A well tested and comprehensive Golang statistics library / package / module with no dependencies. If you have any s

Dec 30, 2022

Golang io.Reader and io.Writer but with limits

LimitIO io.Reader and io.Writer with limit.

Dec 14, 2022

Cogger is a standalone binary and a golang library that reads an internally tiled geotiff

Cogger is a standalone binary and a golang library that reads an internally tiled geotiff (optionally with overviews and masks) and rewrites it

Dec 12, 2022

A Golang and Python solution for Queue-it's Proof-of-Work challenge.

Queue-it Proof-of-Work A Golang and Python solution for Queue-it's Proof-of-Work challenge (https://queue-it.com/blog/proof-of-work-block-bad-bots/).

Oct 16, 2022

⚙️ Concept of Golang HTML render engine with frontend components and dynamic behavior

SSC Engine An HTML render engine concept that brings frontend-like components experience to the server side with native html/template on steroids. Sup

Nov 25, 2022

solution lock for golang, locallock and remote lock base on redis.

solution lock for golang, locallock and remote lock base on redis.

Dec 19, 2022
Related tags
🍕 Enjoy a slice! A utility library for dealing with slices and maps that focuses on type safety and performance.

?? github.com/elliotchance/pie Enjoy a slice! pie is a library of utility functions for common operations on slices and maps. Quick Start FAQ What are

Dec 30, 2022
A Go package for checking conditions for slices and maps.

check Go package The check package of Go helps one to check various conditions for slices: []int []float64 []string []bool maps: map[string]int map[st

Aug 26, 2022
Create deep copies (clones) of your maps and slices without using reflection.

DeepCopy DeepCopy helps you create deep copies (clones) of your maps and slices. Create deep copies (clones) of your objects The package is based on t

Nov 20, 2022
Bitwise AND on two byte-slices using SIMD instructions

This package provides a vectorised function which performs bitwise AND operation on all pairs of elements in two byte-slices. It detects CPU instruction set and chooses the available best one (AVX512, AVX2, SSE2).

Oct 17, 2022
A go1.18+ package to (maybe) simplify performing operations on slices in a fluent-like style.

sop ✨ W.I.P. ✨ sop (slices operation) is a go1.18+ package to (maybe) simplify performing operations on slices in a fluent-like style with common oper

Oct 1, 2022
Access and modify property values in deeply nested maps, using dot-separated paths

Dig lets you access and modify property values in deeply nested, unstructured maps, using dot-separated paths: source := make(map[string]interface{})

May 7, 2022
Automatically creates & tiles .tmx format maps from a world map interface
Automatically creates & tiles .tmx format maps from a world map interface

Autotile Create tiled maps for an arbitrarily large world space from a simple interface, then add larger objects randomly with simple rules (eg. place

Aug 19, 2022
Goridge is high performance PHP-to-Golang codec library which works over native PHP sockets and Golang net/rpc package.
Goridge is high performance PHP-to-Golang codec library which works over native PHP sockets and Golang net/rpc package.

Goridge is high performance PHP-to-Golang codec library which works over native PHP sockets and Golang net/rpc package. The library allows you to call Go service methods from PHP with a minimal footprint, structures and []byte support.

Dec 28, 2022
memresolver is an in-memory golang resolver that allows to override current golang Lookup func literals

mem-resolver memresolver is an in-memory golang resolver that allows to override current golang Lookup func literals How to use it Create your custom

Jun 23, 2022
A Go (golang) library for parsing and verifying versions and version constraints.

go-version is a library for parsing versions and version constraints, and verifying versions against a set of constraints. go-version can sort a collection of versions properly, handles prerelease/beta versions, can increment versions, etc.

Jan 9, 2023