Skip to content
/ gonvert Public

Provide the transformation of map and struct for the go language

License

Notifications You must be signed in to change notification settings

gotail/gonvert

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

刚学Go,练练手。

Go语言Telegram群组欢迎大家加入: https://t.me/joinchat/HVxWPhDHqfmSsA_8dU4goA

gonvert

Provide the transformation of map and struct for the go language

安装

go get -u -t github.com/gotail/gonvert

Struct 转换为 Map

package main

import (
	"fmt"

	"github.com/gotail/gonvert"
)

type Person struct {
	Username string `gonvert:"username"`
	Age      int    `gonvert:"age"`
	Company  string `gonvert:"company"`
}

func main() {
	person := Person{Username: "golang", Age: 10, Company: "Github"}
	PersonMap := gonvert.Struct2Map(&person) // 可传入结构体或结构体指针
	fmt.Println(PersonMap)
}

Map转换为 Struct

package main

import (
	"fmt"

	"github.com/gotail/gonvert"
)

type Person struct {
	Username string `gonvert:"username"`
	Age      int `gonvert:"age"`
	Company  string `gonvert:"company"`
	Jh bool	`gonvert:"jh"`
}

var data = map[string]interface{}{
	"username": "golang",
	"age":      30,
	"company":  "Github",
	"jh":true,
}

func main() {
	person := &Person{}
	gonvert.Map2Struct(data, person)
	fmt.Println(person)
}

About

Provide the transformation of map and struct for the go language

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages