Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/xiaoxin01/typeregistry
create type dynamically in Golang
https://github.com/xiaoxin01/typeregistry
Last synced: about 2 months ago
JSON representation
create type dynamically in Golang
- Host: GitHub
- URL: https://github.com/xiaoxin01/typeregistry
- Owner: xiaoxin01
- License: mit
- Created: 2020-01-14T15:50:38.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2020-02-20T13:00:03.000Z (almost 5 years ago)
- Last Synced: 2024-07-31T20:51:45.542Z (5 months ago)
- Language: Go
- Size: 7.81 KB
- Stars: 23
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-go - typeregistry - A library to create type dynamically. (Generators / Search and Analytic Databases)
- awesome-go-extra - typeregistry - 01-14T15:50:38Z|2020-02-20T13:00:03Z| (Generators / Utility/Miscellaneous)
README
# a library to create type dynamically
[![Report](https://goreportcard.com/badge/github.com/xiaoxin01/typeregistry)](https://goreportcard.com/badge/github.com/xiaoxin01/typeregistry)
[![Build Status](https://travis-ci.org/xiaoxin01/typeregistry.svg?branch=master)](https://travis-ci.org/xiaoxin01/typeregistry)## how to use
```go
// 1. define struct
type Student struct {
Age int
Name string
}// 2. add reflect type
key := AddType(new(Student))// 3. create struct from registed key
student := Make(key)
```## custom registed key
```go
var i interface{} = new(Student)// use lowercase struct name as key
name := AddTypeWithKey(i, func(i interface{}) string {
tpe := reflect.TypeOf(i).Elem()
return strings.ToLower(tpe.Name())
})student, ok := Create("student").(*Student)
```## benchmark
```bash
go.exe test -benchmem -run=^$ supperxin/typeregistry -bench ^BenchmarkMake$goos: windows
goarch: amd64
pkg: supperxin/typeregistry
BenchmarkMake-8 8823256 138 ns/op 64 B/op 2 allocs/op
PASS
ok supperxin/typeregistry 1.551s
``````bash
go.exe test -benchmem -run=^$ supperxin/typeregistry -bench ^BenchmarkCreateDirectly$goos: windows
goarch: amd64
pkg: supperxin/typeregistry
BenchmarkCreateDirectly-8 1000000000 0.593 ns/op 0 B/op 0 allocs/op
PASS
ok supperxin/typeregistry 0.857s
```