ProductPromotion
Logo

Go.Lang

made by https://0x3d.site

GitHub - usk81/generic: flexible data type for Go
flexible data type for Go. Contribute to usk81/generic development by creating an account on GitHub.
Visit Site

GitHub - usk81/generic: flexible data type for Go

GitHub - usk81/generic: flexible data type for Go

Generic

go.dev reference License codecov Go Report Card

flexible data type for Go

support: Go 1.12+

Install

standard go get:

go get -u github.com/usk81/generic/v2

Usage

encode/decode:

package main

import (
	"encoding/json"
	"github.com/usk81/generic/v2"
)

type User struct {
	Name String      `json:"name"`
	Age  generic.Int `json:"age"`
}

var user1 User
u1 := []byte(`{"name":"Daryl Dixon","age":"40"}`)
json.Unmarshal([]byte(u1), &user1)
b, _ := json.Marshal(user1)
Println(string(b))
// {"name":"Daryl Dixon","age":40}

var user2 User
u2 := []byte(`{"name":"Rick Grimes"}`)
json.Unmarshal([]byte(u2), &user2)
b, _ := json.Marshal(user2)
Println(string(b))
// {"name":"Rick Grimes","age":null}

set:

package main

import (
	"fmt"
	"github.com/usk81/generic"
)

func main() {
	v := 1.0

	var tb generic.Bool
	tb.Set(v)
	vb := tb.Weak()
	fmt.Printf("%v, (%T)\n", vb, vb)
	// true, (bool)

	var tf generic.Float
	tf.Set(v)
	vf := tf.Weak()
	fmt.Printf("%v, (%T)\n", vf, vf)
	// 1, (float64)

	var ti generic.Int
	ti.Set(v)
	vi := ti.Weak()
	fmt.Printf("%v, (%T)\n", vi, vi)
	// 1, (int64)

	var ts generic.String
	ts.Set(v)
	vs := ts.Weak()
	fmt.Printf("%v, (%T)\n", vs, vs)
	// 1, (string)

	var tt generic.Time
	tt.Set(v)
	vt := tt.Weak()
	fmt.Printf("%v, (%T)\n", vt.UTC(), vt)
	// 1970-01-01 09:00:01 +0900 JST, (time.Time)

	var tu generic.Uint
	tu.Set(v)
	vu := tu.Weak()
	fmt.Printf("%v, (%T)\n", vu, vu)
	// 1, (uint64)
}

Benchmarks

Marshal

Bool

version requests /op B/op allocs/op
1.0.0 5000000 240 ns 185 3
2.0.0 200000000 6.69 ns 0 0

Float

version requests /op B/op allocs/op
1.0.0 3000000 425 ns 192 3
2.0.0 5000000 260 ns 64 3

Int

version requests /op B/op allocs/op
1.0.0 5000000 265 ns 192 3
2.0.0 20000000 70.5 ns 16 2

String (small)

version requests /op B/op allocs/op
1.0.0 3000000 382 ns 200 3
2.0.0 20000000 89.0 ns 128 2

String (Large)

version requests /op B/op allocs/op
1.0.0 1000000 1056 ns 776 4
2.0.0 5000000 237 ns 896 2

Time

version requests /op B/op allocs/op
1.0.0 1000000 1122 ns 360 5
2.0.0 3000000 401 ns 48 1

TimestampMS

version requests /op B/op allocs/op
1.0.0 20000000 97.9 ns 32 2
2.0.0 20000000 91.2 ns 32 2

TimestampNano

version requests /op B/op allocs/op
1.0.0 10000000 114 ns 64 2
2.0.0 10000000 112 ns 64 2

Timestamp

version requests /op B/op allocs/op
1.0.0 20000000 88.4 ns 32 2
2.0.0 20000000 86.7 ns 32 2

Uint

version requests /op B/op allocs/op
1.0.0 5000000 277 ns 192 3
2.0.0 20000000 64.2 ns 16 2

Licence

MIT

Author

Yusuke Komatsu

Articles
to learn more about the golang concepts.

Resources
which are currently available to browse on.

mail [email protected] to add your project or resources here ๐Ÿ”ฅ.

FAQ's
to know more about the topic.

mail [email protected] to add your project or resources here ๐Ÿ”ฅ.

Queries
or most google FAQ's about GoLang.

mail [email protected] to add more queries here ๐Ÿ”.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory