Skip to content

RekeningkuDev/nill

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

nill

Github Actions GoDoc Go Report Card

Features

  • Support for echo (web framework) https://echo.labstack.com/
  • The zero-value is 0, and is safe to use without initialization
  • Addition, subtraction, multiplication, division, modulo and etc.
  • JSON serialization/deserialization
  • Support for Custom JSON Encoder/Decoder

Install

Run go get github.com/RekeningkuDev/nill

Requirements

nill library requires Go version >=1.18

Usage

package main

import (
	"encoding/json"
	"fmt"
	"github.com/RekeningkuDev/nill"
	goJson "github.com/goccy/go-json"
)

func init() {
	// Custom JSON unmarshal or Unmarshal
	nill.JSONMarshal = goJson.Marshal
	nill.JSONUnmarshal = goJson.Unmarshal
}

func main() {
	price := nill.NewFloat64(50.99)
	// Add 
	subtotal := price.Mul(nill.NewFloat64(8))
	fmt.Println("Subtotal:", subtotal)    // Subtotal: 407.92
	
	// Custom type
	type product struct {
		Name nill.String `json:"name"`
		Price nill.Float64 `json:"price"`
    }
	
	type information struct {
		Product nill.Type[product] `json:"product"`
		Qty nill.Int `json:"qty"`
    }
	
	// JSON serialization/deserialization
	p := information{
		Product: nill.Type[product]{
			Valid: true,
			Value: product{
				Name: nill.NewString("Product A"),
				Price: nill.NewFloat64(50.99),
			},
		},
		Qty: nill.NewInt(8),
	}
	
	// Marshal
	b, err := json.Marshal(p)
	if err != nil {
        fmt.Println(err)
	}
	
	fmt.Println(string(b)) // {"product":{"name":"Product A","price":50.99},"qty":8}
}

Documentation

http://godoc.org/github.com/RekeningkuDev/nill

License

The MIT License (MIT)

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages