Convert struct, slice, array, map or others for Golang

XConv

zh-CN

XConv is a golang type convertor. It convert any value between types (base type, struct, array, slice, map, etc.)

Features

  • Convert between integer types
  • Convert between float types
  • Convert between array or slice
  • Convert between two map
  • Convert between time to string or integer types (int32, int64, int)
  • Convert between struct types (same field name, or custom)
  • Custom rule
  • Global custom rule

Usage

Convert directly:

var src = []int32{1, 2, 3}
var dst []int
xconv.Convert(src, &dst)

Convert time to integer:

var src = time.Now()
var dst int64
xconv.Convert(src, &dst)

Convert time to string by custom display (default display is "2006-01-02 15:04:05"):

display := "2006-01-02 15"
var src = time.Now()
var dst string
xconv.NewConvertor(src).TimeFormat(display).Apply(&dst)

Convert struct to another (It will convert same field name, include anonymous fields. Fields must be exportable):

type SrcStruct{
    A int32
}

type DstStruct{
    A int
}

var src = SrcStruct{A: 1}
var dst DstStruct
xconv.Convert(src, &dst)

Custom converting rule:

type SrcStruct{
    A int
}

type DstStruct{
    A string
}

var src = SrcStruct{A: 1}
var dst DstStruct
NewConvertor(src).
    Rule(xconv.IntTypes, reflect.String,
        func(c *xconv.Convertor, src, dst reflect.Value) {
            dst.SetString(strconv.Itoa(int(src.Int())))
        }).
    Apply(&dst)

Or custom field rule:

type SrcStruct{
    A int
}

type DstStruct{
    B int64
}

var src = SrcStruct{A: 1}
var dst DstStruct
NewConvertor(src).
    Field("B",
        func(srcObj SrcStruct) int64 {
            return int64(srcObj.A)
        })
    Apply(&dst)

Custom global rule:

import (
    "reflect"
    "strconv"

    "github.com/howcrazy/xconv"
)

func init(){
    xconv.ConvertMap.
        TimeFormat("2006-01-02 15:04").
        Set(xconv.IntTypes, xconv.StringTypes,
            func(c *Convertor, src, dst reflect.Value) {
                dst.SetString(strconv.Itoa(int(src.Int())))
            })
}
Similar Resources

💪 Helper Utils For The Go: string, array/slice, map, format, cli, env, filesystem, test and more.

💪 Helper Utils For The Go: string, array/slice, map, format, cli, env, filesystem, test and more.

💪 Helper Utils For The Go: string, array/slice, map, format, cli, env, filesystem, test and more. Go 的一些工具函数,格式化,特殊处理,常用信息获取等等

Jan 6, 2023

G-array is a GoLang library, that contains the generic function to do the array operations.

G-array Garray is a library written in Go (Golang). Which have a collection of functions to do the array operations. Installation To install G-array p

Oct 3, 2022

Tugas Alta Immersive Backend Golang Basic Programming Part 4(Complexity Analysis, Array, Slice, Function)

Tugas Alta Immersive Backend Golang Basic Programming Part 4(Complexity Analysis, Array, Slice, Function)

Tatacara Melakukan Setup Tugas clone project ini dengan cara git clone https://github.com/Immersive-Backend-Resource/Basic-Programming-Part4.git sete

Jan 9, 2022

Tugas Alta Immersive Backend Golang Basic Programming Part 3(Array, Slice, Function)

Tugas Alta Immersive Backend Golang Basic Programming Part 3(Array, Slice, Function)

Tatacara Melakukan Setup Tugas clone project ini dengan cara git clone https://github.com/Immersive-Backend-Resource/Basic-Programming-Part3.git sete

Jan 9, 2022

Repo Tugas Basic Programming Part 4(Complexity Analysis, Array, Slice, Function) ALTA Immersive BE5

Cara mengerjakan tugas hari ke 4 clone project ini dengan cara git clone https://github.com/ALTA-Immersive-BE5/Basic-Programming-Part4.git setelah cl

Dec 15, 2021

Repo Tugas Basic Programming Part 3(Array, Slice, Function) ALTA Immersive BE5

Cara mengerjakan tugas hari ke 3 clone project ini dengan cara git clone https://github.com/jackthepanda96/Basic-Programming-Part3.git setelah clone

Dec 15, 2021

☄ The golang convenient converter supports Database to Struct, SQL to Struct, and JSON to Struct.

☄ The golang convenient converter supports Database to Struct, SQL to Struct, and JSON to Struct.

Gormat - Cross platform gopher tool The golang convenient converter supports Database to Struct, SQL to Struct, and JSON to Struct. 中文说明 Features Data

Dec 20, 2022

sleuth checks that you declared a slice with length and you are trying append to the slice.

sleuth sleuth detects when an append is used on a slice with an initial size. Instruction go install github.com/sivchari/sleuth/cmd/sleuth Usage packa

Sep 15, 2021

:steam_locomotive: Decodes url.Values into Go value(s) and Encodes Go value(s) into url.Values. Dual Array and Full map support.

Package form Package form Decodes url.Values into Go value(s) and Encodes Go value(s) into url.Values. It has the following features: Supports map of

Dec 26, 2022

TIled map editor CSV export conversion to C array

tiled2c This tool is very simplistic and is a variation of the one written to convert between Tiled map editor CSV exports and a format for the sega s

Nov 28, 2021

Slice - provides generic Map, Reduce and Filter functions for Go.

slice slice is a simple Go package to provide generic versions of Map, Reduce and Filter on slices. I mainly wrote it as an exercise to get more famil

Jan 1, 2023

A faster method to get elements from an interface (Struct or Slice type) for Go.

A faster method to get elements from an interface (Struct or Slice type) for Go.

May 13, 2022

A go Library for scan database/sql rows to struct、slice、other types. And it support multiple databases connection management

ploto A go Library for scan database/sql rows to struct、slice、other types. And it support multiple databases connection management It's not an ORM. wo

Nov 3, 2022

skipmap is a high-performance concurrent sorted map based on skip list. Up to 3x ~ 10x faster than sync.Map in the typical pattern.

skipmap is a high-performance concurrent sorted map based on skip list. Up to 3x ~ 10x faster than sync.Map in the typical pattern.

Introduction skipmap is a high-performance concurrent map based on skip list. In typical pattern(one million operations, 90%LOAD 9%STORE 1%DELETE), th

Jan 8, 2023

Recursively searches a map[string]interface{} structure for another map[string]interface{} structure

msirecurse Recursively searches a map[string]interface{} structure for existence of a map[string]interface{} structure Motivation I wrote this package

Mar 3, 2022

Grpc-gateway-map-null - gRPC Gateway test using nullable values in map

Demonstrate gRPC gateway behavior with nullable values in maps Using grpc-gatewa

Jan 6, 2022

Convert Golang Struct To GraphQL Object On The Fly

Straf Convert Golang Struct To GraphQL Object On The Fly Easily Create GraphQL Schemas Example Converting struct to GraphQL Object type UserExtra stru

Oct 26, 2022

Convert json string to Golang struct

json-to-go-cli Convert json string to Golang struct How to install git clone https://github.com/tiancheng92/json-to-go-cli.git cd json-to-go-cli go bu

May 10, 2022

⚔ Go package for data validation and filtering. support Map, Struct, Form data. Go通用的数据验证与过滤库,使用简单,内置大部分常用验证、过滤器,支持自定义验证器、自定义消息、字段翻译。

Validate The package is a generic Go data validate and filter tool library. Support quick validate Map, Struct, Request(Form, JSON, url.Values, Upload

Dec 28, 2022
Map, Reduce, Filter, De/Multiplex, etc. for the Go language.

proto proto gives Go operations like Map, Reduce, Filter, De/Multiplex, etc. without sacrificing idiomatic harmony or speed. It also introduces a conv

Nov 17, 2022
Several functional programming supporting in golang

A golang library that makes operations on slice easilier What can I do? slice process Map Filter Sort Reverse map process Keys Values output (starting

Jun 27, 2022
Data syncing in golang for ClickHouse.
Data syncing in golang for ClickHouse.

ClickHouse Data Synchromesh Data syncing in golang for ClickHouse. based on go-zero ARCH A typical data warehouse architecture design of data sync Aut

Jan 1, 2023
indodate is a plugin for golang programming language for date convertion on indonesian format

indodate is a package for golang programming language for date conversion on indonesian format

Oct 23, 2021
Basic Crud operation api's in golang

Basic Crud operation api's in golang

Nov 9, 2021
:100:Go Struct and Field validation, including Cross Field, Cross Struct, Map, Slice and Array diving

Package validator Package validator implements value validations for structs and individual fields based on tags. It has the following unique features

Jan 1, 2023
💯 Go Struct and Field validation, including Cross Field, Cross Struct, Map, Slice and Array diving

Package validator implements value validations for structs and individual fields based on tags.

Nov 9, 2022
Takes an integer array, returns the array sorted and the number of inversions in the array

Takes an integer slice, returns the slice sorted and the number of inversions in the slice

Jan 25, 2022
Golang parameter validation, which can replace go-playground/validator, includes ncluding Cross Field, Map, Slice and Array diving, provides readable,flexible, configurable validation.
Golang parameter validation, which can replace go-playground/validator, includes ncluding Cross Field, Map, Slice and Array diving, provides readable,flexible, configurable validation.

Checker 中文版本 Checker is a parameter validation package, can be use in struct/non-struct validation, including cross field validation in struct, elemen

Dec 16, 2022