Skip to content

Latest commit

 

History

History
69 lines (52 loc) · 1.1 KB

README.md

File metadata and controls

69 lines (52 loc) · 1.1 KB

刚学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)
}