Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

74 Commits

Repository files navigation

Generic

go.dev referenceLicensecodecovGo 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"
)
typeUserstruct {
NameString`json:"name"`Age generic.Int`json:"age"`
}
varuser1Useru1:= []byte(`{"name":"Daryl Dixon","age":"40"}`)
json.Unmarshal([]byte(u1), &user1)
b, _:=json.Marshal(user1)
Println(string(b))
// {"name":"Daryl Dixon","age":40}varuser2Useru2:= []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"
)
funcmain() {
v:=1.0vartb generic.Booltb.Set(v)
vb:=tb.Weak()
fmt.Printf("%v, (%T)\n", vb, vb)
// true, (bool)vartf generic.Floattf.Set(v)
vf:=tf.Weak()
fmt.Printf("%v, (%T)\n", vf, vf)
// 1, (float64)varti generic.Intti.Set(v)
vi:=ti.Weak()
fmt.Printf("%v, (%T)\n", vi, vi)
// 1, (int64)varts generic.Stringts.Set(v)
vs:=ts.Weak()
fmt.Printf("%v, (%T)\n", vs, vs)
// 1, (string)vartt generic.Timett.Set(v)
vt:=tt.Weak()
fmt.Printf("%v, (%T)\n", vt.UTC(), vt)
// 1970-01-01 09:00:01 +0900 JST, (time.Time)vartu generic.Uinttu.Set(v)
vu:=tu.Weak()
fmt.Printf("%v, (%T)\n", vu, vu)
// 1, (uint64)
}

Benchmarks

Marshal

Bool

versionrequests/opB/opallocs/op
1.0.05000000240 ns1853
2.0.02000000006.69 ns00

Float

versionrequests/opB/opallocs/op
1.0.03000000425 ns1923
2.0.05000000260 ns643

Int

versionrequests/opB/opallocs/op
1.0.05000000265 ns1923
2.0.02000000070.5 ns162

String (small)

versionrequests/opB/opallocs/op
1.0.03000000382 ns2003
2.0.02000000089.0 ns1282

String (Large)

versionrequests/opB/opallocs/op
1.0.010000001056 ns7764
2.0.05000000237 ns8962

Time

versionrequests/opB/opallocs/op
1.0.010000001122 ns3605
2.0.03000000401 ns481

TimestampMS

versionrequests/opB/opallocs/op
1.0.02000000097.9 ns322
2.0.02000000091.2 ns322

TimestampNano

versionrequests/opB/opallocs/op
1.0.010000000114 ns642
2.0.010000000112 ns642

Timestamp

versionrequests/opB/opallocs/op
1.0.02000000088.4 ns322
2.0.02000000086.7 ns322

Uint

versionrequests/opB/opallocs/op
1.0.05000000277 ns1923
2.0.02000000064.2 ns162

Licence

MIT

Author

Yusuke Komatsu

About

flexible data type for Go

Topics

Resources

Stars

49 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages